我希望能够在用户给定的时区中显示时间戳。我注意到,即使使用像 date-fns-tz 这样的专用库,它似乎也会返回没有意义的值。
在幕后他们显然使用 Intl,当我使用该模块时,它似乎没有提供正确的值。
const zones = ['PST', 'MST', 'CST', 'EST'];
zones.forEach((timeZone) =>
console.log(
new Intl.DateTimeFormat('en-US', {
timeZone,
timeStyle: 'full',
dateStyle: 'full',
}).format(1588743894000)
)
);
输出:
Tuesday, May 5, 2020 at 10:44:54 PM Pacific Daylight Time Tuesday, May 5, 2020 at 10:44:54 PM GMT-07:00 Wednesday, May 6, 2020 at 12:44:54 AM Central Daylight Time Wednesday, May 6, 2020 at 12:44:54 AM GMT-05:00
但这不应该是四个不同的值吗?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您正在指定三字母时区缩写,它期望的是 IANA 时区 (https://tc39.es/ecma402/#sec-time-zone-names)。我相信它很困惑,因为您正在通过白天的标准时区。
const zones = ["America/Los_Angeles", "America/Denver", "America/Chicago", "America/New_York"] zones.forEach(timeZone => console.log(new Intl.DateTimeFormat('en-US', { timeZone, timeStyle: "full", dateStyle: "full"}).format(1588743894000)));这会产生: