Parsing a Youtube API Date in Javascript Parsing a Youtube API Date in Javascript json json

Parsing a Youtube API Date in Javascript


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.