Luxon DateTime fromISO 比 moment.js 少一小时

Luxon DateTime fromISO off by one hour versus moment.js

正在尝试用 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 在这种情况下会产生不同的输出?

let foo1 = moment(activeToDateTimeString).utcOffset(-4, true);

这会更正您的代码,但是当您搬到 Luxon 时,夏令时的变化不会影响您将来。

现在(2020 年 3 月 19 日)纽约比 UTC 晚 4 小时,因为从 2020 年 3 月 8 日开始,它从东部标准时间进入东部夏令时。

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