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.
Combining jacks and joe larson's response, the following code worked well for me:
$scope.formatDate = function(date){
var date = date.split("-").join("/");
var dateOut = new Date(date);
return dateOut;
};
works well for Chrome and Safari... :)
Thanks Jack and joe larson!!!
Older Internet Explorer versions (and apparently your version of Safari) won't parse a yyyy-mm-dd string, such as new Date('2012-12-31')
.
Another option is jQuery.datepicker.parseDate(), although that may be a bit overkill.
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.
var date = "2012-12-31".split("-");
var your_date = new Date(date[0], date[1]-1, date[2]);
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.
In the case of using a PHP web service, I would recommend returning your dates using
$row['date'] = date('j/n/Y',strtotime($row['date']));
to have normal functionality with safari/ie.
You're better off parsing the date string yourself:
function dateFromISO( str ) {
var d = null;
str.replace(/^(\d\d\d\d)-(\d\d)-(\d\d)$/, function(_, y, m, d) {
d = new Date(parseInt(y, 10), parseInt(m, 10) - 1, parseInt(d, 10));
});
return d;
}
redeemableDate = dateFromISO( redeemableDate );
Even if the other browsers liked those date strings, you'd have the problem of them always being interpreted as UTC. For me, for example, when I pass that string "2012-12-31" to Firefox, it tells me that the date is 30 Dec 2012, because I'm 6 hours behind UTC. In other words, "2012-12-31" is interpreted as midnight of that date, UTC time. Assuming you want everybody in the world to see the right date, if you construct the Date object with numbers it's assumed to be local time at the client.