gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:17:06 29 4
gpt4 key购买 nike

此文档提到 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/ )

如何从 moment.js 获取 ISO 8601?

最佳答案

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

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

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

我打开了 issue因为我认为这会导致意想不到的结果。

关于javascript - 如何在 moment.js 中将日期格式化为 ISO 8601?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25725019/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com