ISO 8601日期间JS pretation差异 - IE / FF与铬 [英] ISO 8601 Date JS Interpretation Difference - IE/FF versus Chrome

查看:175
本文介绍了ISO 8601日期间JS pretation差异 - IE / FF与铬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么IE / FF和Chrome的JavaScript引擎如何跨preT 此的日期格式?( YYYY-MM-DDTHH:MM:SS.FFF 的),而不时区标志

 新的日期(2015-02-18T15:43:57.803)。getUTCHours()

UTC时间


  

铬: 15
  

   IE11 / FF: 21


我不明白这一点 - 是不是因为Chrome的假定它是当地的,而IE / FF假设它是UTC?这似乎是一个Chrome的错误。

有趣的是 - 附加一个Z的字符串的结尾告诉Chrome和IE / FF的时间是UTC,他们能同意。有没有其他人注意到了这个JavaScript实现差异与日期

 新的日期。(2015-02-18T15:43:57.803Z)getUTCHours()

UTC时间


  

铬: 15
  

   IE11 / FF: 15


最后 - 这是的出的现成的串行的ASP.NET Web API ,我认为用JSON.NET,但现在看来是内部的,其中JSON.NET使用<一个href=\"https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs\"相对=nofollow> IsoDateTimeConverter

检查 GlobalConfiguration.Configuration.Formatters.JsonFormatter 告诉我们使用我的 JsonMediaTypeFormatter 。不使用JSON.NET串行开箱即用的Web API?

这是Web API的人的福音 - 至少早在ASP.NET MVC我们有一个一致的日期格式(尽管专有的 - /日期刻度(数)/ )通过<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx\"相对=nofollow> 的JavaScriptSerializer


解决方案

ES5 说,如果没有一个时区的ISO 8601格式的日期应被视为地方,但是的 编辑。草案6 说他们当作UTC。有些脚本引擎已经实现了编辑。 6,有些ES5其余的都不是。

的编辑。 6草案与ISO 8601规范是一致的。

底线是不使用的 Date.parse 的(或通过字符串Date构造函数),手工解析日期字符串。

Why do IE/FF and Chrome javascript engines differ on how to interpret this Date format (YYYY-MM-DDTHH:mm:ss.fff) without the timezone designator?

new Date("2015-02-18T15:43:57.803").getUTCHours()

UTC Hours

Chrome: 15
IE11/FF: 21

I don't understand this - is it because Chrome assumes it's local whereas IE/FF assume it's UTC? This seems like a Chrome bug.

Interestingly - appending a "Z" to the end of the string tells both Chrome and IE/FF that the time is UTC and they can agree. Has anyone else noticed this javascript implementation discrepancy with Date?

new Date("2015-02-18T15:43:57.803Z").getUTCHours()

UTC Hours

Chrome: 15
IE11/FF: 15

Ultimately - this is the result of the out-of-box serializer for ASP.NET Web API, which I thought used JSON.NET, but now appears to be internal where JSON.NET uses IsoDateTimeConverter.

Checking GlobalConfiguration.Configuration.Formatters.JsonFormatter tells me we're using JsonMediaTypeFormatter. Is Web API not using JSON.NET serializer out of the box?

This is a boon for Web API people - at least back in ASP.NET MVC we had a consistent date format (albeit proprietary - /Date(number of ticks)/) via the JavascriptSerializer

解决方案

ES5 says that ISO 8601 format dates without a time zone should be treated as local, but the ed. 6 draft says to treat them as UTC. Some script engines have implemented ed. 6, some ES5 and the rest neither.

The ed. 6 draft is consistent with the ISO 8601 specification.

The bottom line is don't use Date.parse (or pass strings to the Date constructor), manually parse date strings.

这篇关于ISO 8601日期间JS pretation差异 - IE / FF与铬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆