Coder Perfect

In JavaScript, how can I create an ISO 8601 formatted string?

Problem

I’ve got a Date object. How do I render the title portion of the following snippet?

<abbr title="2010-04-02T14:12:07">A couple days ago</abbr>

I borrowed the section on “relative time in words” from another library.

The following are some of the things I’ve tried:

function isoDate(msSinceEpoch) {

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

}

However, this provides me with:

"2010-4-2T3:19"

Asked by James A. Rosen

Solution #1

There is already a toISOString() function:

var date = new Date();
date.toISOString(); //"2011-12-19T15:28:46.493Z"

If you’re using a browser that doesn’t support it, don’t worry: I’ve got you covered:

Answered by Anatoly Mironov

Solution #2

See the last example on https://developer.mozilla.org/en/Core JavaScript 1.5 Reference:Global Objects on page https://developer.mozilla.org/en/Core JavaScript 1.5 Reference:Global Objects: Date:

/* Use a function for the exact format desired... */
function ISODateString(d) {
    function pad(n) {return n<10 ? '0'+n : n}
    return d.getUTCFullYear()+'-'
         + pad(d.getUTCMonth()+1)+'-'
         + pad(d.getUTCDate())+'T'
         + pad(d.getUTCHours())+':'
         + pad(d.getUTCMinutes())+':'
         + pad(d.getUTCSeconds())+'Z'
}

var d = new Date();
console.log(ISODateString(d)); // Prints something like 2009-09-28T19:03:12Z

Answered by 3 revs, 3 users 64%

Solution #3

Almost every to-ISO method on the web drops the timezone information by applying a convert to “Z”ulu time (UTC) before outputting the string. Browser’s native .toISOString() also drops timezone information.

This wastes significant information because the server or recipient may always convert a full ISO date to Zulu time or whatever timezone it needs while still receiving the sender’s timezone information.

I’ve found that using the Moment.js javascript package and the following code is the best solution:

To obtain the current ISO time, with timezone and millisecond information

now = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ")
// "2013-03-08T20:11:11.234+0100"

now = moment().utc().format("YYYY-MM-DDTHH:mm:ss.SSSZZ")
// "2013-03-08T19:11:11.234+0000"

now = moment().utc().format("YYYY-MM-DDTHH:mm:ss") + "Z"
// "2013-03-08T19:11:11Z" <- better use the native .toISOString() 

To retrieve the ISO time of a native JavaScript Date object without milliseconds but with timezone information

var current_time = Date.now();
moment(current_time).format("YYYY-MM-DDTHH:mm:ssZZ")

This can be used in conjunction with Date.js to create functions such as Date.today(), the result of which can then be supplied to moment.

A date string structured in this manner is JSON compliant and lends itself well to database storage. Python and C# appear to enjoy it.

Answered by Daniel F

Solution #4

The query was in ISO format, albeit with less accuracy. Voila:

 new Date().toISOString().slice(0, 19) + 'Z'
 // '2014-10-23T13:18:06Z'

If the trailing Z isn’t required, simply leave it out.

Answered by arcseldon

Solution #5

The shortest, although Internet Explorer 8 and previous do not support it:

new Date().toJSON()

Answered by younes0

Post is based on https://stackoverflow.com/questions/2573521/how-do-i-output-an-iso-8601-formatted-string-in-javascript