简体   繁体   中英

javascript new Date() or moment.js parse date wrong

I have a DateTime value that comes from server in the following format:

2015-08-16T01:29:00.000Z  

However when I do

new Date('2015-08-16T01:29:00.000Z')

the result is this:

Sun Aug 16 2015 04:29:00 GMT+0300 (FLE Daylight Time)

please notice the hour is wrong, instead of 01:29 AM is 04:29 AM

Same for moment.js it get's the wrong hour.

How can I solve this?

It's the same time -- the server gives it to you as UTC (hence the Z at the end) and Javascript then helpfully shows it in the local timezone of the browser, FLE Daylight Time. Given that both times clearly state which timezone they are, nothing is going wrong, strictly speaking.

What to do depends on what you want exactly. By doing

moment.utc('2015-08-16T01:29:00.000Z');

You put moment.js in UTC mode, showing everything in UTC (if I understand the docs correctly).

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