new Date() broken on Chrome 67

2020-02-06 09:43发布

Starting Chrome 67 (full version is 67.0.3396.87), I am experiencing weird behaviour with creation of a new Date object. Smallest reproducible case, goes something like:

    <html>
    <body>    
    <script>    alert(new Date(-62135596800000));        </script>
    </body>
    </html>

On Firefox 60.0.2 the alert message is:

Mon Jan 01 0001 00:00:00 GMT+0000 (GMT Standard Time)

On Internet Explorer 11 and Edge 41.16299.461.0, the alert message is same as Firefox:

Mon Jan 01 0001 00:00:00 GMT+0000 (GMT Standard Time)

However, on Chrome 67 I see:

Sun Dec 31 0000 23:58:45 GMT-0001 (Greenwich Mean Time)

Edit: JsFiddle

Edit2 Turns out it's nothing to do with Microsoft's library.

标签: javascript
2条回答
叼着烟拽天下
2楼-- · 2020-02-06 10:31

Depending on the response to that bug report, I think this is actually due to Chrome possibly including IANA timezone information. Browsers, time zones, Chrome 67 Error

For example, when I run that fiddle, I get Sun Dec 31 0000 18:09:24 GMT-0550 (Central Standard Time) which corresponds to the IANA entry Zone America/Chicago -5:50:36 - LMT 1883 Nov 18 12:09:24.

So this is a "feature" not a bug I think. They are using the more "accurate" historical time offsets instead of current day time offsets for historical dates.

You can view the data here : https://github.com/eggert/tz just look for your appropriate world location file and try and avoid all the commented out lines unless you are morbidly curious about the history of your time zones.

What you can do to "fix" it so it display more or less correctly is to call .toUTCString() on the Date object which will force it to UTC time and display Mon, 01 Jan 0001 00:00:00 GMT as @Pointy pointed out in the comments on the initial question.

查看更多
Animai°情兽
3楼-- · 2020-02-06 10:45

Your code, as long as MS's one, doesn't know anything about the timezone, so I assume Chrome takes the default one.

I guess it should be better if you create your date as below:

const date = new Date(Date.UTC(year, month, day, hour, minute, second));

查看更多
登录 后发表回答