不确定日期解析失踪时区 [英] Inconsistant date parsing with missing timezone

查看:100
本文介绍了不确定日期解析失踪时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行一些测试时,我发现浏览器之间出现了不一致的行为,具有以下javascript

In doing some testing I've found inconsistant behavior between browsers with the following javascript

new Date("2013-09-10T08:00:00").toString()

在IE和Firefox中,结果是



In IE and Firefox the result is


Tue Sep 10 2013 08:00:00 GMT-0400(Eastern Daylight Time)

"Tue Sep 10 2013 08:00:00 GMT-0400 (Eastern Daylight Time)"

在Chrome中,结果是

In Chrome the result is


Tue Sep 10 2013 04:00:00 GMT -0400(Eastern Daylight Time)

"Tue Sep 10 2013 04:00:00 GMT-0400 (Eastern Daylight Time)"

所以根据我阅读的 ECMA 脚本的日期字符串的格式,它说...

So according to my reading of the ECMA script of the format for Date strings it says...


所有数字必须为基数10.如果MM或DD字段不存在,则01用作
作为值。如果HH,mm或ss字段不存在,则00用作
作为值,缺省sss字段的值为000。
缺少时区偏移量的值为Z

但是在文档中对于new Date()构造函数,它说

However in the documentation for the "new Date()" constructor it says


15.9.3.2 new Date(value)

15.9.3.2 new Date (value)


  1. 让v为ToPrimitive(value)。

  2. 如果Type(v)为String,则

  1. Let v be ToPrimitive(value).
  2. If Type(v) is String, then

a。解析v作为日期,与解析方法完全相同(15.9.4.2);让V为此日期的
值。

a. Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2); let V be the time value for this date.

15.9.4.2 Date.parse(string)

15.9.4.2 Date.parse (string)

解析函数将ToString运算符应用于其参数,并将生成的字符串解释为日期
和时间;它返回一个Number,UTC时间值对应于
的日期和时间。字符串可能会被解释为本地时间,UTC
时间,或某个其他时区的时间,具体取决于
the String的内容。

The parse function applies the ToString operator to its argument and interprets the resulting String as a date and time; it returns a Number, the UTC time value corresponding to the date and time. The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String.

任何想法哪个实现是正确的?

Any ideas which implementation is correct?

推荐答案

标准冲突。 ISO 8601 表示

Standards clash. ISO 8601 states that:


如果没有给出UTC时间表示的UTC关系信息,则假定时间在当地时间。

If no UTC relation information is given with a time representation, the time is assumed to be in local time.

ECMA


缺席时区偏移量的值为Z。

The value of an absent time zone offset is "Z".

Mozilla开发者认为ISO优先,Chrome人似乎不同意。

Mozilla devs think that ISO takes precedence, Chrome folks seem to disagree.

目前的ES6草案表示(在20.3.1.15之下):

The current draft of ES6 says (under 20.3.1.15):


如果时区偏移不存在,则日期时间被解释为本地时间。 >

If the time zone offset is absent, the date-time is interpreted as a local time.

所以Mozilla的实现(将)正确。

so Mozilla's implementation is (will be) correct.

这篇关于不确定日期解析失踪时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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