简体   繁体   English

javascript new Date()或moment.js解析日期错误

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

I have a DateTime value that comes from server in the following format: 我有一个来自服务器的DateTime值,格式如下:

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 请注意小时是错误的,而不是01:29 AM is 04:29 AM

Same for moment.js it get's the wrong hour. 与moment.js一样,这是错误的时间。

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. 同时,服务器将它作为UTC(因此以Z结尾)提供给您,然后Javascript会在浏览器的本地时区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). 您将moment.js放在UTC模式下,以UTC显示所有内容(如果我正确理解了文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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