luxon 将本地时间转换为 UTC 给定时区

Wil*_*llD 9 javascript luxon

数据源有一个没有偏移量的 ISO-8601 日期时间字段。

例子: 2019-07-09T18:45

但是,我知道有问题的时间被理解为时America/Chicago区。

如何在 UTC 中获得与该时间等效的 Luxon DateTime 对象?

我可以做 DateTime.fromISO('2019-07-09T18:45').plus({hours: 5})......但这只会在夏令时的半年(比如现在)有效。否则偏移量将是.plus({hours: 6})

Luxon 是否有一个日期感知(因此 DST 感知)的方法来从特定的分区本地时间转换为 UTC?

Vin*_*zoC 12

由于您知道输入日期的时区,因此您可以zone在解析它时使用该选项。正如fromISO文档所述:

public static fromISO(text: string, opts: Object): DateTime
Run Code Online (Sandbox Code Playgroud)

opts.zone:如果在输入字符串本身中没有指定偏移量,则使用此区域。

然后您可以使用toUTC将您的 DateTime 转换为 UTC:

将日期时间的区域“设置”为 UTC。返回一个新构造的 DateTime。

相当于setZone ('utc')

这是一个实时示例:

public static fromISO(text: string, opts: Object): DateTime
Run Code Online (Sandbox Code Playgroud)
const DateTime = luxon.DateTime;
const d = DateTime.fromISO('2019-07-09T18:45', {zone: 'America/Chicago'});
console.log(d.toISO());
console.log(d.toUTC().toISO());
Run Code Online (Sandbox Code Playgroud)