I tested the following code in firefox scratchpad and got interesting result?
var date=new Date("2012-05-12");
var date2 = new Date("05/12/2012");
date;
/*
Fri May 11 2012 17:00:00 GMT-0700 (Pacific Daylight Time)
*/
date2;
/*
Sat May 12 2012 00:00:00 GMT-0700 (Pacific Daylight Time)
*/
Two dates are different. Apparently this is due to the timezone issue. What I want is date2 result. How can I make js engine correctly treats the ISO date style?
I think the issue is that the string “2012-05-12” is taken to be an ISO 8601 date, while “05/12/2012” is an RFC 2822 date. In the ISO format, the lack of a timezone implies UTC. At midnight on the morning off May 12, in California (or wherever you are) it’s 7 PM the previous evening.
The RFC date without a time zone, however, is parsed under the assumption that you want the timestamp for midnight in your local timezone. (Well, not necessarily your timezone; the timezone of the computer where your JavaScript runs 🙂
You can see the difference if you pass those strings to
Date.parse().The RFC date format can include an explicit time zone, but the ISO format cannot. (Well, it can, but browsers don’t pay attention, and apparently IE doesn’t handle those at all.)
edit — here’s a simple (dumb; no error checking) function that’ll give you a date from that 3-part ISO form: