简体   繁体   中英

Javascript Date constructor behaves differently in IE and Chrome

I have set my system time to UTC.

IE 10

new Date("2014-06-07T19:00:00") //Sat Jun 7 19:00:00 UTC +0100 2014

Chrome 35

new Date("2014-06-07T19:00:00") //Sat Jun 07 2014 20:00:00 GMT +0100 (GMT Daylight Time)

Why is the result different by 1 hour? 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.1.15

The value of an absent time zone offset is “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)

Edit:

@Dark Falcon yes, toISOString produces different results for the first string below

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

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. 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”.

This comment new Date() works differently in Chrome and Firefox answers the question.

It is a bug in the ECMAScript 5.1 spec 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.

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".

The draft version ECMAScript 6 ( http://people.mozilla.org/~jorendorff/es6-draft.html ) changes this sentence to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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