How do I format a date as ISO 8601 in moment.js?
JavascriptMomentjsDate FormattingIso8601Javascript Problem Overview
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?
Javascript Solutions
Solution 1 - Javascript
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 - Javascript
Use format
with no parameters:
var date = moment();
date.format(); // "2014-09-08T08:02:17-05:00"
Solution 3 - Javascript
Also possible with vanilla JS
new Date().toISOString() // "2017-08-26T16:31:02.349Z"
Solution 4 - Javascript
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 - Javascript
var date = moment(new Date(), moment.ISO_8601);
console.log(date);
Solution 6 - Javascript
If you just want the date portion (e.g. 2017-06-27), and you want it to work regardless of time zone and also in Arabic, here is code I wrote:
function isoDate(date) {
if (!date) {
return null
}
date = moment(date).toDate()
// don't call toISOString because it takes the time zone into
// account which we don't want. Also don't call .format() because it
// returns Arabic instead of English
var month = 1 + date.getMonth()
if (month < 10) {
month = '0' + month
}
var day = date.getDate()
if (day < 10) {
day = '0' + day
}
return date.getFullYear() + '-' + month + '-' + day
}
Solution 7 - Javascript
Answer in 2020 (Includes Timezone Support)
The problem we were having is that, by default, ISOStrings aren't localized to your timezone. So, this is kinda hacky, but here's how we ended up solving this issue:
/** Imports Moment for time utilities. */
const moment = require("moment-timezone")
moment().tz("America/Chicago").format()
//** Returns now in ISO format in Central Time */
export function getNowISO() {
return `${moment().toISOString(true).substring(0, 23)}Z`
}
This will leave you with an exact ISO-formatted, localized string.
Important note: Moment now suggests using other packages for new projects.
Solution 8 - Javascript
var x = moment();
//date.format(moment.ISO_8601); // error
moment("2010-01-01T05:06:07", ["YYYY", moment.ISO_8601]);; // error
document.write(x);
Solution 9 - Javascript
If you need the formatting string : YYYY-MM-DDTHH:mm:ssZ
var date = moment();
console.log(date.format("YYYY-MM-DDTHH:mm:ssZ"));