JavaScript and ISO dates

July 21 2012

Tags: javascript

I'm sure at one time or another any front-end web developer has run up against cross browser JavaScript discrepancies. In particular, implementations of the Date API have a great amount of variance, sometimes in direct violation of standards.

Today, my issue is with the various implementations of the Date constructor deal with ISO 8601 standard date time strings.

I should never have assumed

According to the Wikipedia article , when the timezone or location is unspecified then the date time is represented as local time.

The major browsers don't seem to agree on this.

Chrome 20:

new Date("2007-04-05T14:30");
Thu Apr 05 2007 09:30:00 GMT-0500 (CDT)

It seems Chrome interprets the date as UTC no matter the absence of a UTC designator.

Opera 12:

new Date("2007-04-05T14:30");
2007-04-05T09:30:00.000

Opera seems to do the same thing.

Internet Explorer 9:

new Date("2007-04-05T14:30");
Thu Apr 5 09:30:00 CDT 2007

Internet Explorer seems to agree.

Firefox 14:

new Date("2007-04-05T14:30");
Thu Apr 05 2007 14:30:00 GMT-0500 (CDT)

Okay, Firefox seems to match the ISO description instead of the other browsers.

Safari 5:

new Date("2007-04-05T14:30");
Invalid Date

new Date("2007-04-05T14:30:00");
Invalid Date

new Date("2007-04-05T14:30:00Z");
Thu Apr 05 2007 09:30:00 GMT-0500 (CDT)

No support at all in Safari for a non UTC designator for an ISO 8601 date string.

You may be thinking that no sane person would assume local time anyway. I agree, you should be as explicit as possible with your date representations. However, I only ran into this because I had to deal with legacy code which DID assume local time for an internal site. My point being... Shit happens.

What happens if we do it right?

Across the board, modern browsers support UTC designated ISO 8601 time strings (Something like this: 2007-04-05T14:30:00Z). Meaning that in the future anyone can rely on a nice human readable ISO 8601 date time string for date representation.

Aren't we forgetting something?

There is at least one hole in this plan. The biggest of which is Internet Explorer 8. Though it's 6 years old at the time of writing, it was shipped with the initial version of Windows 7. Meaning... it will probably be around for some time to come.

Internet explorer's Date object does not have an ISO 8601 date string constructor at all.

What the hell am I supposed to do then?

Well, you can always use epoch time representations. The JavaScript Date API in almost every browser out there supports this.

I have taken to just using a Date wrapper/helper library called moment.js. Some of moment.js' more spectacular features:

  • True cross browser support for ISO 8601
  • A working date parser with format specification
  • Date formatting API
  • Date delta helper functions
  • Internationalization
  • Anything else you would want to do with a date

Although it's yet another JavaScript library to load, moment.js lets me get back to work and stop worrying about dates.

Previous post


Comments