【问题标题】:Luxon DateTime fromISO off by one hour versus moment.js与 moment.js 相比,来自 ISO 的 Luxon DateTime 减少了一小时
【发布时间】:2020-06-30 17:55:55
【问题描述】:

尝试用 Luxon 替换我的 Angular 应用程序中的 moment.js 以减小包大小。

我遇到过两个库产生不同输出的情况,我不知道为什么。

moment.js 产生一个提前一小时的日期。

const activeToDateTimeString = '2014-08-06T13:07:04';

let foo1 = moment(activeToDateTimeString).utcOffset(-5, true);
let foo2 = DateTime.fromISO(activeToDateTimeString, {zone: 'America/New_York'}).setZone('America/New_York', { keepLocalTime: true });
let foo3 = DateTime.fromJSDate(new Date(activeToDateTimeString)).setZone('America/New_York', { keepLocalTime: true });
let foo4 = DateTime.fromISO(activeToDateTimeString).setZone('America/New_York', { keepLocalTime: true });

console.log(foo1.toDate());
console.log(foo2.toJSDate());
console.log(foo3.toJSDate());
console.log(foo4.toJSDate());

输出:

Wed Aug 06 2014 14:07:04 GMT-0400 (Eastern Daylight Time)
Wed Aug 06 2014 13:07:04 GMT-0400 (Eastern Daylight Time)
Wed Aug 06 2014 13:07:04 GMT-0400 (Eastern Daylight Time)
Wed Aug 06 2014 13:07:04 GMT-0400 (Eastern Daylight Time)

为什么在这种情况下 moment.js 会产生不同的输出?

【问题讨论】:

    标签: momentjs luxon


    【解决方案1】:
    let foo1 = moment(activeToDateTimeString).utcOffset(-4, true);
    

    这将更正您的代码,但是当您迁移到 Luxon 时,日光时间的更改将不会影响您的未来。

    现在(2020 年 3 月 19 日)纽约从 2020 年 3 月 8 日开始比东部标准时间晚 4 小时。

    如果此时纽约处于东部标准时间,您的代码将输出相同的时间。

    【讨论】:

      猜你喜欢
      • 2020-06-11
      • 2014-02-19
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2012-07-28
      • 2021-07-02
      • 1970-01-01
      相关资源
      最近更新 更多