gpt4 book ai didi

javascript - 如何从 moment.unix() 将 moment 解析为特定时区

转载 作者:行者123 更新时间:2023-12-02 21:44:32 27 4
gpt4 key购买 nike

我有一个时间戳的初始值(在 User-1 的时区中创建),我正在从 firebase 的 firestore 中检索。其格式为 Unix 纪元的 UTC 秒数。

我的目标是获取初始时间戳值,并将其转换为当前用户的时区。如果我使用

moment.unix(initial_In_Seconds).format("H:mma") 我得到了正确的初始时间。使用 .unix() 是我能够获得正确初始时间的唯一方法。现在我需要将其转换为特定的时区。假设“美国/丹佛”(初始时区是 GMT -05:00)

到目前为止,我还没有在任何合并中成功使用 moment.tz()

我已经尝试过:

moment.tz(moment.unix(initial_In_Seconds).format("H:mma"), "America/Denver")

let unix = moment.unix(initial_In_Seconds).format("H:mma");
let parsed = moment.tz(unix, "America/Denver");

如何解析这个? Moment.js 让我困惑

最佳答案

如果您的时区偏移是使用 IANA 代表位置(例如“美国/丹佛”)指定的,那么您可以使用 toLocaleString ,前提是这些位置不是太模糊(即不受支持) ECMAScript 实现可能会运行您的代码),例如

function timeValueWithTimezone(unixOffset, loc) {
let d = new Date(unixOffset * 1000);
return d.toLocaleString(void 0, {
hour12: true,
hour: 'numeric',
minute: '2-digit',
timeZone: loc
});
}

let timeValue = 1582090120;
let loc = 'America/Denver';

console.log('At your local time: ' +
new Date(timeValue * 1000).toLocaleString(void 0, {
hour12: true,
hour: 'numeric',
minute: '2-digit'
}));
console.log('Denver local time is: ' + timeValueWithTimezone(timeValue, loc));

关于javascript - 如何从 moment.unix() 将 moment 解析为特定时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60291047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com