momentjs toISOString without the "z"

momentjs toISOString without the "z"

本文关键字:quot the toISOString without momentjs      更新时间:2023-09-26

我需要创建ISO-8601日期。我正在使用的时刻

moment(my_date).toISOString() 创建一个 ISO 日期,结果类似于这样:

2015-03-17T15:12:38.076Z

我需要将此日期发送到一个 API,该 API 希望日期略有不同(尽管文档说他们使用的是 ISO-8601):

2015-03-17T15:12:38.076-4:00 

有没有办法使用 momentjs 或其他东西获得预期的格式?

编辑:我找到了一个解决方案:

moment.tz(my_date, "America/Argentina/Buenos_Aires").format("YYYY-MM-DDTHH:mm:ss.SSSZ")

您可以使用 momentjs 时区:http://momentjs.com/timezone/

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var london     = newYork.clone().tz("Europe/London");
newYork.format();    // 2014-06-01T12:00:00-04:00
london.format();     // 2014-06-01T17:00:00+01:00

z启动 UTC 时间戳,API 期望与 UTC 的差异,因此-4:00。如果您确实想要时区之间的转换,momentjs 时区是我建议的方法。

但是moment().format();不是将时间返回为2014-09-08T08:02:17-05:00吗?