gpt4 book ai didi

javascript - 缺少时区的日期解析不一致

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:48:52 26 4
gpt4 key购买 nike

在做一些测试时,我发现使用以下 javascript 的浏览器之间的行为不一致

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

在 IE 和 Firefox 中结果是

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

在 Chrome 中,结果是

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

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

All numbers must be base 10. If the MM or DD fields are absent "01" is used as the value. If the HH, mm, or ss fields are absent "00" is used as the value and the value of an absent sss field is "000". The value of an absent time zone offset is "Z"

但是在“new Date()”构造函数的文档中它说

15.9.3.2 new Date (value)

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

    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)

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.

有什么想法是正确的吗?

最佳答案

标准冲突。 ISO 8601 states that :

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

ECMA says :

The value of an absent time zone offset is “Z”.

Mozilla 开发人员 think ISO 优先,Chrome 用户似乎不同意。

current draft of ES6说(在 20.3.1.15 下):

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

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

关于javascript - 缺少时区的日期解析不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19278797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com