如何使用矩格式获取时区

Posted

技术标签:

【中文标题】如何使用矩格式获取时区【英文标题】:How to get time zone using moment format 【发布时间】:2019-03-03 05:51:41 【问题描述】:

我需要使用 moment 获取当前时区 我已经给出了这样的代码

console.log(moment.utc(new Date()).format('Z'),moment.utc(new Date()),'moment')

得到类似的结果

我想获得 +0530,但现在我获得 +00:00,我们怎么才能获得呢!

【问题讨论】:

简单地使用moment()而不是moment.utc() 【参考方案1】:

您应该简单地使用moment() 而不是moment.utc()

默认情况下,moment 解析并显示为当地时间。

如果要解析或显示 UTC 时刻,可以使用 moment.utc() 而不是 moment()

这为我们带来了 Moment.js 的一个有趣特性。 UTC 模式。

在 UTC 模式下,所有显示方式都会以 UTC 时间显示,而不是本地时间。

另请参阅Local vs UTC vs Offset 指南。

【讨论】:

以上是关于如何使用矩格式获取时区的主要内容,如果未能解决你的问题,请参考以下文章