如何清除与时刻时区对象关联的时区

How to clear the timezone associated with a moment-timezone object

使用 moment-timezone,如果您有一个带有时区的 moment 对象,例如moment.tz('America/New_York'),如何清除时区并将时刻更改为 "local" 时间?

值得注意的是,moment.local() 不起作用(对 moment.tz() 的后续调用仍然等于 America/New_York)。我可以确认我的当地时间不是 America/New_York.

我找到的唯一解决办法是设置一个无效的时区,例如moment.tz('local')。这会向控制台记录一条警告,表明您已尝试设置无效时区(烦人),但它会清除与时刻对象关联的时区,并将其返回到本地时刻对象。 moment-timezone 发出的警告强调该解决方案将来可能会崩溃,而且显然是 hack。

完成此任务的正确方法是什么?

更新

此问题是由 moment-timezone 中的错误引起的。从版本开始,0.5.24moment.local() 应该正确清除与 moment-timezone 对象关联的时区。

原创

此时...此刻!这似乎是 moment-timezone 中的错误(参见 #738, #258)。我发现的当前解决方法是首先使用 moment.utc()(它清除时区并将时间设置为 "utc" 模式),然后使用 moment.local()。 IE。 moment.utc().local().