How do I format a date as ISO 8601 in moment.js?

362,738

Solution 1

moment().toISOString(); // or format() - see below

http://momentjs.com/docs/#/displaying/as-iso-string/

Update Based on the answer: by @sennet and the comment by @dvlsg (see Fiddle) it should be noted that there is a difference between format and toISOString. Both are correct but the underlying process differs. toISOString converts to a Date object, sets to UTC then uses the native Date prototype function to output ISO8601 in UTC with milliseconds (YYYY-MM-DD[T]HH:mm:ss.SSS[Z]). On the other hand, format uses the default format (YYYY-MM-DDTHH:mm:ssZ) without milliseconds and maintains the timezone offset.

I've opened an issue as I think it can lead to unexpected results.

Solution 2

Use format with no parameters:

var date = moment();
date.format(); // "2014-09-08T08:02:17-05:00"

(http://jsfiddle.net/8gvhL1dz/)

Solution 3

Also possible with vanilla JS

new Date().toISOString() // "2017-08-26T16:31:02.349Z"

Solution 4

When you use Mongoose to store dates into MongoDB you need to use toISOString() because all dates are stored as ISOdates with miliseconds.

moment.format() 

2018-04-17T20:00:00Z

moment.toISOString() -> USE THIS TO STORE IN MONGOOSE

2018-04-17T20:00:00.000Z

Solution 5

var date = moment(new Date(), moment.ISO_8601);
console.log(date);
Share:
362,738

Related videos on Youtube

sennett
Author by

sennett

Mainly JS.

Updated on February 25, 2022

Comments

  • sennett
    sennett about 2 years

    This docs mention moment.ISO_8601 as a formatting option (from 2.7.0 - http://momentjs.com/docs/#/parsing/special-formats/), but neither of these work (even 2.7.0):

    var date = moment();
    date.format(moment.ISO_8601); // error
    moment.format(date, moment.ISO_8601); // error
    

    (http://jsfiddle.net/b3d6uy05/1/)

    How can I get an ISO 8601 from moment.js?

    • joews
      joews over 9 years
      What version of moment are you using? The docs say that constant was added in 2.7.0.
    • sennett
      sennett over 9 years
      @joews 2.7.0. Clarified in question. See my answer I figured it out. Docs not hugely clear though.
    • joews
      joews over 9 years
      Yeah, they only mention special formats in the context of parsing. Odd.
    • Saahithyan Vigneswaran
      Saahithyan Vigneswaran about 7 years
      u can try moment().toISOString()
  • dvlsg
    dvlsg over 8 years
    Just as a side note, these two answers are not the same, even though they both fulfill ISO format requirements. date.toISOString() will keep the milliseconds and use utc, date.format() will drop the milliseconds and use your local timezone (or at least, that's the behavior I am currently getting in chrome -- jsfiddle.net/8gvhL1dz/22 )
  • Greivin López
    Greivin López about 7 years
    I agree with you @Yashua. I think the use of "format()" should be avoided because is not intuitive. Also I don't think a function that just wraps the native "toISOString()" should exist at all. That being said, perhaps giving a new function like: "toISO8601()" with the option to keep the timezone and a proper documentation will be better.
  • StinkyCat
    StinkyCat almost 7 years
    If you want the utc time, but formatted your own way, instead of ISO8601, you can do the following: moment().utc().format("OUTPUT_FORMAT")
  • Matt Sgarlata
    Matt Sgarlata almost 7 years
    Using .format() with an Arabic locale leads to Arabic symbols rather than English ones, which is probably undesirable.
  • Brice
    Brice over 6 years
    Useful whn formating a date in kibana but you don't have access to JS.
  • JoeTidee
    JoeTidee about 5 years
    Only if you want it in UTC, without maintaining the timezone.
  • JoeTidee
    JoeTidee about 5 years
    toISOString does not output in your local timezone - it is always in (zero offset) UTC.
  • Benny Neugebauer
    Benny Neugebauer almost 5 years
    If you want to maintain the local timezone, use moment().toISOString(true);.
  • borchvm
    borchvm about 4 years
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained.
  • Stephan Hogenboom
    Stephan Hogenboom over 3 years
    Can you elaborate your answer a bit?