0

我正在使用 luxon 根据 timzone 以毫秒为单位转换日期。我正在接收 ISO 格式的时间,但是当我应用 luxon 来转换日期时,它确实如此,但是当我以毫秒为单位解析它时,我与当前和指定的 timzone 没有毫秒差异。

console.log("== Local Zone == ");
console.log(DateTime.local().zoneName);
const sd = DateTime.fromISO("2019-07-27T07:37:49.211Z");
console.log(sd.toMillis());

console.log("== Updated Zone == ");
Settings.defaultZoneName = "America/Chicago";
console.log(DateTime.local().zoneName);
const hd = DateTime.fromISO("2019-07-27T07:37:49.211Z");
console.log(hd.toMillis());

任何帮助/建议将不胜感激。

4

1 回答 1

0

您将获得相同的值 ( 1564213069211),因为输入字符串 ( "2019-07-27T07:37:49.211Z") 以Z代表 UTC 偏移量的结尾。

因此,Luxon 只需考虑 UTC+0 偏移量并相应地显示结果。

const DateTime = luxon.DateTime;
const Settings = luxon.Settings;
console.log("== Local Zone == ");
console.log(DateTime.local().zoneName);
const sd = DateTime.fromISO("2019-07-27T07:37:49.211Z");
console.log(sd.toMillis());
const sdNoZ = DateTime.fromISO("2019-07-27T07:37:49.211");
console.log(sdNoZ.toMillis());

console.log("== Updated Zone == ");
Settings.defaultZoneName = "America/Chicago";
console.log(DateTime.local().zoneName);
const hd = DateTime.fromISO("2019-07-27T07:37:49.211Z");
console.log(hd.toMillis());
const hdNoZ = DateTime.fromISO("2019-07-27T07:37:49.211");
console.log(hdNoZ.toMillis());
<script src="https://cdn.jsdelivr.net/npm/luxon@1.25.0/build/global/luxon.js"></script>

于 2020-11-16T09:08:57.490 回答