简体   繁体   English

Javascript日期构造函数在IE和Chrome中的行为有所不同

[英]Javascript Date constructor behaves differently in IE and Chrome

I have set my system time to UTC. 我已将系统时间设置为UTC。

IE 10 IE 10

new Date("2014-06-07T19:00:00") //Sat Jun 7 19:00:00 UTC +0100 2014 new Date("2014-06-07T19:00:00") //星期六6月7日19:00:00 UTC +0100 2014

Chrome 35 Chrome 35

new Date("2014-06-07T19:00:00") //Sat Jun 07 2014 20:00:00 GMT +0100 (GMT Daylight Time) new Date("2014-06-07T19:00:00") // 2014年6月7日星期六20:00:00 GMT +0100(GMT日光时间)

Why is the result different by 1 hour? 为什么结果与1小时不同? The date constructor is part of the specification so it should be standard across browsers? 日期构造函数是规范的一部分,因此它应该是跨浏览器的标准?

http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.3.2 http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.3.2

http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15

The value of an absent time zone offset is “Z”. 缺席时区偏移的值是“Z”。

(I have figured out how to fix the problem - appending the timezone 'Z' - "2014-06-07T19:00:00Z" produces consistent results, but I am interested in why this is happening in the first place) (我已经想出如何解决这个问题 - 追加时区'Z' - “2014-06-07T19:00:00Z”会产生一致的结果,但我感兴趣的是为什么会出现这种情况)

Edit: 编辑:

@Dark Falcon yes, toISOString produces different results for the first string below @Dark Falcon是的,toISOString为下面的第一个字符串产生不同的结果

http://jsfiddle.net/25UcJ/2/ http://jsfiddle.net/25UcJ/2/

<p id=container></p>

container.innerHTML = 
new Date("2014-06-07T19:00:00").toISOString() +
'<br>' +
new Date("2014-01-07T19:00:00").toISOString() +
'<br>'+
new Date("2014-06-07T19:00:00Z").toISOString();

IE IE

2014-06-07T18:00:00.000Z
2014-01-07T19:00:00.000Z
2014-06-07T19:00:00.000Z

Chrome

2014-06-07T19:00:00.000Z
2014-01-07T19:00:00.000Z
2014-06-07T19:00:00.000Z

Edit: 编辑:

Not sure this is a duplicate of new Date() works differently in Chrome and Firefox , I am not asking how to fix the problem, I am asking why is there a difference in the first place. 不确定这是重复的新Date()在Chrome和Firefox中的工作方式不同 ,我不是在问如何解决这个问题,我在问为什么第一个地方有区别。 The accepted answer to that question says 'your input is wrong'. 该问题的公认答案是“你的输入是错误的”。 The second answer says 'the spec is imprecise'. 第二个答案说“规格不精确”。 I don't think either are correct - note my quote from the spec "The value of an absent time zone offset is “Z”. 我不认为其中任何一个是正确的 - 请注意我的规范中的引用“缺席时区偏移的值是”Z“。

This comment new Date() works differently in Chrome and Firefox answers the question. 此评论新的Date()在Chrome和Firefox中的工作方式不同,可以回答这个问题。

It is a bug in the ECMAScript 5.1 spec https://bugs.ecmascript.org/show_bug.cgi?id=112 . 这是ECMAScript 5.1规范https://bugs.ecmascript.org/show_bug.cgi?id=112中的一个错误。 From the comments on the bug, it appears that Chrome 35 implements what the spec says (literally), while IE 10 implements what the spec actually intended to say. 从对bug的评论来看,Chrome 35似乎实现了规范所说的(字面意思),而IE 10实现了规范实际上要说的内容。

The bug relates to the same sentence in the spec mentioned in the question: 该错误与问题中提到的规范中的相同句子有关:

ECMA-262 edition 5.1 (p. 179): The value of an of absent time zone offset is "Z". ECMA-262版本5.1(p.179):缺少时区偏移的值为“Z”。

The draft version ECMAScript 6 ( http://people.mozilla.org/~jorendorff/es6-draft.html ) changes this sentence to: 草案版ECMAScript 6( http://people.mozilla.org/~jorendorff/es6-draft.html )将此句改为:

If the time zone offset is absent, the date-time is interpreted as a local time. 如果不存在时区偏移,则将日期时间解释为本地时间。

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

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