Youtube's API returns a JSON object with an array of videos. Each video object has a published date formatted like "2012-01-11T20:49:59.415Z". If I initialize a Javascript Date object using the code below, the object returns "Invalid Date".
var dt = new Date( "2012-01-11T20:49:59.415Z" );
I'm using this on iOS/mobile Safari, if that makes a difference.
Any suggestions or ideas on how to create a valid object?
Try using JavaScript's Date.parse(string)
and the Date
constructor which takes the number of milliseconds since the epoch. The "parse" function should accept a valid ISO8601 date on any browser.
For example:
var d = new Date(Date.parse("2012-01-11T20:49:59.415Z"));
d.toString(); // => Wed Jan 11 2012 15:49:59 GMT-0500 (EST)
d.getTime(); // => 1326314999415
var dt = "2012-01-11T20:49:59.415Z".replace("T"," ").replace(/\..+/g,"")
dt = new Date( dt );
I ended up finding a solution at http://zetafleet.com/blog/javascript-dateparse-for-iso-8601. It looks like the date is in a format called 'ISO 8601.' On earlier browsers (Safari 4, Chrome 4, IE 6-8), ISO 8601 is not supported, so Date.parse doesn't work. The code referenced from the linked blog post extends the current Date class to support ISO 8601.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With