Hegwin.Me

In silence I feel full; With speech I sense emptiness.

Differrent behavior of new Date between Firefox and Chrome

new Date()在Firefox和Chome下不同效果

I've been working on a project where I need to draw charts, and the JS library I'm using is D3js. I've encountered an amazing problem where the points and lines that show up in Chrome don't show up in Firefox.

Initially, I looked at the reason and found that the point coordinates were calculated differently. Our X-axis is the time axis. The X-coordinate calculation under Chrome from date is smooth; while in Firefox, the coordinate became a negative value, resulting in the inability to display it on the graph.

After a closer look, I found that the return value of new Date() was different in the two browsers.

Firefox 37.0.1

new Date("03/23/15, 03:35 AM")
=> Date {Tue Mar 23 1915 03:35:00 GMT+0800 (CST)}

Chrome 41.0.2272.118 (64-bit)

new Date("03/23/15, 03:35 AM")
=> Mon Mar 23 2015 03:35:00 GMT+0800 (CST)

After comparing, we conclude that if we use a two-digit year passed to new Date(), like 15, Firefox will calculate it as 1915, while Chrome will calculate it as 2015. And if it is 88, both Firefox and Chome will count it as 1988.

< Back