How do I output an ISO-8601 formatted string in Javascript?

Posted by James A. Rosen on Stack Overflow See other posts from Stack Overflow or by James A. Rosen
Published on 2010-04-04T04:04:13Z Indexed on 2010/04/04 4:13 UTC
Read the original article Hit count: 267

Filed under:
|
|

I have a date object from which I'd like to render an HTML snippet like <abbr title="2010-04-02T14:12:07">A couple days ago</abbr>. I have the "relative time in words" portion from another library. How do I render the title portion?

I've tried the following:

isoDate: function(msSinceEpoch) {
  var d = new Date(msSinceEpoch);
  return d.getUTCFullYear() + '-' + (d.getUTCMonth() + 1) + '-' + d.getUTCDate() + 'T'
         d.getUTCHours() + ':' + d.getUTCMinutes() + ':' + d.getUTCSeconds();
}

But that gives me

"2010-4-2T"

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about datetime