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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25725019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:46:28  来源:igfitidea点击:

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

javascriptmomentjsdate-formattingiso8601

提问by sennett

This docs mention moment.ISO_8601as 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?

回答by cyberwombat

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

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

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

UpdateBased on the answer: by @sennet and the comment by @dvlsg (see Fiddle) it should be noted that there is a difference between formatand toISOString. Both are correct but the underlying process differs. toISOStringconverts 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, formatuses the default format (YYYY-MM-DDTHH:mm:ssZ) without milliseconds and maintains the timezone offset.

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

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

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

回答by sennett

Use formatwith no parameters:

format不带参数使用:

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

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

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

回答by artnikpro

Also possible with vanilla JS

也可以使用 vanilla JS

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

回答by webmaster

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

回答by user393274

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
}

回答by nishith

var x = moment();

//date.format(moment.ISO_8601); // error

moment("2010-01-01T05:06:07", ["YYYY", moment.ISO_8601]);; // error
document.write(x);