Moment.js使用操作(add/sub)返回错误的TZ

Moment.js using manipulation (add/sub) returns the wrong TZ

本文关键字:sub 返回 错误 TZ add js 操作 Moment      更新时间:2023-09-26

我的问题非常类似于这个线程:UTC日期之间的Moment.js差异我看到这个bug已经修复了(2年前ha https://github.com/moment/moment/issues/261)

我经历了一个奇怪的&在使用2.5.1版的"moment.js" &0.0.2版"moment-timezones.js"

var now  = moment.tz("2014-03-20 12:00", "EST5EDT"); // date & a pre-defined TZ
console.log(now.format());  // 2014-03-20T12:00:00-04:00

使用此顺序时发现问题:

now.utc()                   // Converting to UTC
console.log(now.format());  // 2014-03-20T16:00:00+00:00
now.subtract('days', 3);    // Subtract
console.log(now.format());  // 2014-03-17T16:00:00-04:00

问题:原来的TZ(-04:00)在"减去"后突然分配了

通过首先"减去"现在"然后转换为UTC来绕过这个问题

console.log(now.format());  // 2014-03-20T12:00:00-04:00
now.subtract('days', 3);    // Subtract
console.log(now.format());  // 2014-03-17T12:00:00-04:00
now.utc()                   // Converting to UTC
console.log(now.format());  // 2014-03-17T16:00:00+00:00

这是一个错误还是我在错误的方式使用moment() ?

这可能行得通!

moment("2014-03-20 12:00").subtract('days', 3).tz("EST5EDT").format()