In doing some testing I've found inconsistant behavior between browsers with the following javascript

new Date("2013-09-10T08:00:00").toString()

In IE and Firefox the result is

"Tue Sep 10 2013 08:00:00 GMT-0400 (Eastern Daylight Time)"

In Chrome the result is

"Tue Sep 10 2013 04:00:00 GMT-0400 (Eastern Daylight Time)"

So according to my reading of the ECMA script of the format for Date strings it says...

All numbers must be base 10. If the MM or DD fields are absent "01" is used as the value. If the HH, mm, or ss fields are absent "00" is used as the value and the value of an absent sss field is "000". The value of an absent time zone offset is "Z"

However in the documentation for the "new Date()" constructor it says

15.9.3.2 new Date (value)

  1. Let v be ToPrimitive(value).
  2. If Type(v) is String, then

    a. Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2); let V be the time value for this date.

15.9.4.2 Date.parse (string)

The parse function applies the ToString operator to its argument and interprets the resulting String as a date and time; it returns a Number, the UTC time value corresponding to the date and time. The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String.

Any ideas which implementation is correct?

up vote 5 down vote accepted

Standards clash. ISO 8601 states that:

If no UTC relation information is given with a time representation, the time is assumed to be in local time.

ECMA says:

The value of an absent time zone offset is “Z”.

Mozilla devs think that ISO takes precedence, Chrome folks seem to disagree.

The current draft of ES6 says (under 20.3.1.15):

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

so Mozilla's implementation is (will be) correct.

  • Chrome seems to have swapped to the ISO definition now. – Matty J Apr 17 at 0:49

There are several questions on stackoverflow.com that address this issue. I gave a rather thorough explanation here if anyone reading this is interested in the browser-to-browser details.

The bottom line though is, for now at least, you should either avoid the ISO 8601 format all together or ALWAYS include a timezone specifier when using it. And, never use the 'YYYY-MM-dd' format because it gets interpreted as a short version of ISO 8601 without a time zone specifier.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.