Is there a string format of a date that a Date object can be built from (i.e. new Date('date string')) that works in all browsers and localities/timezones?
We’ve tried using the format suggested in the HTML5 spec of yyyy-mm-ddThh:mm:ss+hh:mm, but ran into issues with Safari and IE. We’ve tried using the less-formal format of mm/dd/yyyy hh:ss:mm which works but, as far as I know, “month before day” formatting can be ambiguous if your locale settings assume the first number to be a day (non-American countries). Also, it doesn’t attach a timezone which would have issues with different timezones.
In short that’s an almost impossible promise to make as the implementation is UA dependent. I would recommend using the iso8601 format as it will be standard going forward and use a shim to aid in the date parsing here is one I wrote for my own use:
Now instead use
Date.fromString(....)and everyone will be happy in the land of OZ!works in all major browsers, used these references:
http://dev.w3.org/html5/spec/common-microsyntaxes.html
http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
http://msdn.microsoft.com/en-us/library/windows/apps/ff743760(v=vs.94).aspx
http://msdn.microsoft.com/en-us/library/windows/apps/wz6stk2z(v=vs.94).aspx
http://msdn.microsoft.com/en-us/library/windows/apps/k4w173wk(v=vs.94).aspx
https://connect.microsoft.com/IE/feedback/details/723740/date-parse-and-new-date-fail-on-valid-formats
hope this helps -ck
UPDATE: (1/21/2013)
It should be noted that the behavior of what’s considered “standards-compliant behavior” is going to change from ECMAScript-262 v5.1 to ECMAScript-262 v6.0. See: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-15.9.1.15
The language went from: The value of an absent time zone offset is “Z”. to If the time zone offset is absent, the date-time is interpreted as a local time.
The pickle right now is: chrome, opera and mobile safari follow ES5.1, whereas IE10, firefox and desktop safari seem to be following ES6. So it’s kind of a split decision right now… So as such there needs to be a second ES6 standard-compliant version of this script, and here ya go: