简体   繁体   中英

Passing Time Zone in Moment

I am trying to change a time in UTC Format with ISO standard in node js, so here is what I did

moment("201602160530","YYYYMMDDHHmm").toISOString();

"201602160530":- this date is in IST Format. "YYYYMMDDHHmm":- format of date passed.

When I ran this line in my local then output came:-

'2016-02-16T00:00:00.000Z'

This output is valid output as I am converting IST to UTC and with my local timezone in IST, it's working perfectly well.

But then I ran the same code on server and output came is:-

'2016-02-16T05:30:00.000Z'

As the server running with timezone of UTC, moment is taking input date as UTC and Printing the same Date in ISO format.

But My goal is to pass the date in IST and get the time in UTC.

So how do I need to pass the timezone information with this line

moment("201602160530","YYYYMMDDHHmm").toISOString(); 

I found my answer and here it is:-

var moment=require("moment-timezone");
moment.tz("201002160530","YYYYMMDDHHmm","Asia/Kolkata").toISOString();

So instead of using moment library, I used moment-timezone library which provides the additional methods for supporting with timezones.

Final Output on server:-

'2010-02-16T00:00:00.000Z'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM