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

luxon convert local time to utc given a timezone

数据源有一个没有偏移量的 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?

你可以参考下面的luxon函数:

  1. isInDST https://moment.github.io/luxon/docs/class/src/datetime.js~DateTime.html#instance-get-isInDST

检查当前时间是否基于夏令时!

  1. toUTC : 转换为基于 UTC 时间的函数

https://moment.github.io/luxon/docs/class/src/datetime.js~DateTime.html#instance-method-toUTC

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

public static fromISO(text: string, opts: Object): DateTime

opts.zone: use this zone if no offset is specified in the input string itself.

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

"Set" the DateTime's zone to UTC. Returns a newly-constructed DateTime.

Equivalent to setZone('utc')

这是一个活生生的例子:

const DateTime = luxon.DateTime;
const d = DateTime.fromISO('2019-07-09T18:45', {zone: 'America/Chicago'});
console.log(d.toISO());
console.log(d.toUTC().toISO());
<script src="https://cdn.jsdelivr.net/npm/luxon@1.16.0/build/global/luxon.js"></script>