Chunky | Goodness

Chrome vs Firefox Javascript Date Differences

Spent a fair amount of time wracking my brain over a problem caused by this today. It turns out there are some subtle implementation differences between Firefox and Chrome when parsing date strings:

// Chrome
new Date('2011-10-31'); // Sun Oct 30 2011 00:00:00 GMT+1300 (NZDT)

// Firefox
new Date('2011-10-31'); // Sun Oct 30 2011 13:00:00 GMT+1300 (NZST)
                        //                 ^^^^^^^^
                        //                 Different default hour

When parsing a date as a string without an explicit time, Chrome will default the Date object’s time to midnight local time. Firefox will default it to midnight GMT.

To ensure the same behaviour across both browsers, use the following style of initialiser.

// Note that the month numbers are zero indexed, so Jan is 0, Dec 11.
// Year and day numbers are *not* zero indexed.

new Date(2011, 10-1, 31);
// Returns Mon Oct 31 2011 00:00:00 GMT+1300 in Chrome and firefox.