moment.format('ZZ')返回错误的时区偏移

时间:2017-11-05 21:16:31

标签: momentjs

我有一个字符串"2017-11-04T08:00:00Z"

当我使用moment("2017-11-04T08:00:00Z").format('ZZ');时,返回的字符串为"+1300"而非"Z",正如我所料。

这成为一个问题,因为我试图从各个日期部分的字符串构建一个时刻日期,即moment(date + 'T' + time + ':00' + offset, moment.defaultFormat).format()),并且传入错误的偏移量会产生完全错误的时间。

这是format()函数的错误用法还是这个错误?

1 个答案:

答案 0 :(得分:0)

以这种方式将时区偏移量检索为字符串的正确方法是将parseZone函数与格式函数结合使用,根据Matt Johnson的回答here

moment.parseZone("2017-11-04T08:00:00Z").format('Z');

...返回

"+00:00"

使用上面问题中使用的格式函数将返回始终返回用户时区偏移量。