I have the following date format:
Tue Mar 06 17:45:35 -0600 2012
and I want to parse it using new Date()
in Javascript.
Fortunately this works in Chrome but does not work in IE (returns invalid date).
Any suggestions?
Tried to use:
/**Parses string formatted as YYYY-MM-DD to a Date object.
* If the supplied string does not match the format, an
* invalid Date (value NaN) is returned.
* @param {string} dateStringInRange format YYYY-MM-DD, with year in
* range of 0000-9999, inclusive.
* @return {Date} Date object representing the string.
*/
function parseISO8601(dateStringInRange) {
var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
date = new Date(NaN), month,
parts = isoExp.exec(dateStringInRange);
if (parts) {
month = +parts[2];
date.setFullYear(parts[1], month - 1, parts[3]);
if (month != date.getMonth() + 1) {
date.setTime(NaN);
}
}
return date;
}
In ECMA-262 ed 3, Date.parse was entirely implementation dependent. In ES5, only ISO8601 strings should be correctly parsed, anything else is up to the implementation.
Here's a manual parse of the OP format:
Edit
The signs in the timezone line were originally wrong, they're correct now. Oh, and I'm assuming the '-0600' is a javascript timezone offset equivalent to GMT+1000 (e.g. AEST).