I am taking a date from a JSON object in the format of 2012-12-31 and trying to convert it into friendly values and output it.
var redeemableDate = item.Deal.RedeemableDate; //this is coming in the form of 2012-12-31
var redeemableDate = new Date(redeemableDate);
var rdDay = weekday[redeemableDate.getDay()]; //using an array with weekdays
var rdDate = redeemableDate.getDate();
var rdMonth = monthNames[redeemableDate.getMonth()]; //using an array with month names
var rdYear = redeemableDate.getFullYear();
response.write('Valid ' + rdDay + ' ' + rdDate + ' ' + rdMonth + ' ' + rdYear + ' ONLY');
It all works find and dandy in Firefox and Chrome, but Safari and IE (only tested on IE8 so far) don’t like it.
In FF and Chrome I get the expected:
Valid Sunday 2 September 2012 ONLY
But in Safari and IE, I get:
Valid undefined NaN undefined NaN ONLY
When I alert redeemableDate after I have set it as a Date object, Safari returns ‘Invalid Date’ and IE returns ‘NaN’. This is obviously where the issue lies. Is there a way I can get my value into a date object for these browsers?
The yyyy-mm-dd (ISO 8601) date format is not supported in Safari and IE. It is part of ECMAscript 5 though, so it should be just a matter of time.
A solution would be to pass the date in as arguments to Date.
Note that month parameter starts at zero (for January), so you must subtract 1 from the value obtained from the string.
EDIT:
For a shortcut see answer by joe larson below.