【问题标题】:Moment.js using manipulation (add/sub) returns the wrong TZMoment.js 使用操作 (add/sub) 返回错误的 TZ
【发布时间】:2014-03-17 10:08:03
【问题描述】:

我的问题与此线程非常相似:Moment.js diff between UTC dates 我看到错误已修复(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() ?

【问题讨论】:

    标签: javascript momentjs


    【解决方案1】:

    这可能行得通!

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

    【讨论】:

      猜你喜欢
      • 2015-04-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多