如何在moment.js中将日期格式化为ISO 8601?

时间:2022-04-05 16:03:11

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):

这篇文档提到了moment.ISO_8601作为格式化选项(来自2.7.0 - http://momentjs.com/docs/#/parsing/special-formats/),但这些都不起作用(甚至2.7.0):

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

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

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

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

如何从moment.js获得ISO 8601?

5 个解决方案

#1


188  

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

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

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.

更新基于答案:@sennet和@dvlsg的评论(见小提琴)应该注意格式和toISOString之间存在差异。两者都是正确的,但基本过程不同。 toISOString转换为Date对象,设置为UTC然后使用本机Date原型函数以UTC为单位输出ISO8601(YYYY-MM-DD [T] HH:mm:ss.SSS [Z])。另一方面,format使用默认格式(YYYY-MM-DDTHH:mm:ssZ)而没有毫秒,并保持时区偏移。

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

我打开了一个问题,因为我认为这会导致意想不到的结果。

#2


57  

Use format with no parameters:

使用不带参数的格式:

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

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

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

#3


5  

Also possible with vanilla JS

也可以用香草JS

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

#4


3  

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:

如果您只想要日期部分(例如2017-06-27),并且您希望它无论时区和阿拉伯语都能正常工作,这里是我写的代码:

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
}

#5


1  

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

当您使用Mongoose将日期存储到MongoDB时,您需要使用toISOString(),因为所有日期都存储为带有毫秒的ISOdates。

moment.format() 

2018-04-17T20:00:00Z

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

2018-04-17T20:00:00.000Z

#1


188  

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

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

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.

更新基于答案:@sennet和@dvlsg的评论(见小提琴)应该注意格式和toISOString之间存在差异。两者都是正确的,但基本过程不同。 toISOString转换为Date对象,设置为UTC然后使用本机Date原型函数以UTC为单位输出ISO8601(YYYY-MM-DD [T] HH:mm:ss.SSS [Z])。另一方面,format使用默认格式(YYYY-MM-DDTHH:mm:ssZ)而没有毫秒,并保持时区偏移。

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

我打开了一个问题,因为我认为这会导致意想不到的结果。

#2


57  

Use format with no parameters:

使用不带参数的格式:

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

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

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

#3


5  

Also possible with vanilla JS

也可以用香草JS

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

#4


3  

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:

如果您只想要日期部分(例如2017-06-27),并且您希望它无论时区和阿拉伯语都能正常工作,这里是我写的代码:

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
}

#5


1  

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

当您使用Mongoose将日期存储到MongoDB时,您需要使用toISOString(),因为所有日期都存储为带有毫秒的ISOdates。

moment.format() 

2018-04-17T20:00:00Z

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

2018-04-17T20:00:00.000Z