使用moment.js根据RFC 3339格式化时间戳

kev*_*kev 14 momentjs

像这样:

const RFC_3339 = 'YYYY-MM-DDTHH:mm:ss';
moment.utc().format(RFC_3339);
Run Code Online (Sandbox Code Playgroud)

我需要时间戳在结尾处有一个'Z'.有没有更好的方式+'Z'呢?

它应匹配后端的python代码:

RFC_3339_FMT = "%Y-%m-%dT%H:%M:%SZ"
Run Code Online (Sandbox Code Playgroud)

Vin*_*zoC 15

你可以简单地使用format().

正如文档所说:

从版本1.5.0开始,moment#format没有格式的调用将默认为moment.defaultFormat.开箱即用,moment.defaultFormat是ISO8601格式YYYY-MM-DDTHH:mm:ssZ.

从版本2.13.0开始,在UTC模式下,默认格式将Z作为偏移量返回,而不是+00:00

  • @PetrGazarov 请注意 [_UTC 模式_](http://momentjs.com/docs/#/parsing/utc/) 取决于您创建 moment 对象的方式,而不是环境偏移量。`moment().format()` 将在 UTC 服务器上打印 `+00:00`,而 `moment.utc().format()` 将打印 `Z`。有关更多信息,请参阅 [`local()`](http://momentjs.com/docs/#/manipulated/local/) 和 [`utc()`](http://momentjs.com/docs/#/操纵/UTC/)。 (2认同)

tgo*_*tgo 5

我认为该toISOString()功能可以满足您的需求,对吗?请参阅此处文档

  • 不幸的是,我需要没有毫秒的 TS。 (6认同)