Skip to content Skip to sidebar Skip to footer

Ecmascript 5 Date.parse Results For Iso 8601 Test Cases

What result is right for the following test cases? //Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 conso

Solution 1:

According to the ES5 spec, Date.parse will only work with valid ISO 8601 dates. Anything else is implementation dependent (in practice, IE < 9 doesn't work with standard ISO dates, it requires a '/' seperator). So if you feed it an invalid date (such as 2012-11-31) you can get anythying, from 2012-12-01 to an error.

In your tests:

2012-12-31T23:59:60.000Z

should work, though probably not as you expect. Using 60 for seconds indicates a leap second, it isn't equivalent to 24:00:00, only Safari seems to get that right.

Also:

2012-04-04T24:00:00.000Z

should work, it indicates midnight at the end of 4 April, 2012 so Firefox is in error there.

The formats that ES5 implementations should support are in the spec.

Oh, and you should probably also test omission of the 'T' (since it is optional in certain cases that I think include browsers) and different time zones such as:

2012-04-03 23:50:00+10:002012-04-03 23:50:00-04:152012-04-03 23:50:00+1020120403T235000+1000

and so on with YYYYDDD and YYYYWwwD formats, though implementations aren't required to support them.

Post a Comment for "Ecmascript 5 Date.parse Results For Iso 8601 Test Cases"