如何使用偏移值在不同时区显示时间

发布于 2025-01-24 09:32:13 字数 496 浏览 0 评论 0原文

我试图在不同时区显示当前时刻。我尝试使用本机JavaScript和Momk-JS软件包,但似乎您需要时区名称(例如“ America/Toronto”)来显示我想要的信息。问题在于,我目前拥有的信息是所需时区的字符串格式的时间戳(请参见下文),以及时间戳所属的城市。使用城市创建我的TZ值的问题是,我想显示的一个城市不在IANA TZ数据库(Calgary)中。

字符串时间戳:

2022-04-26T14:19:42.8430964-04:00

可以看出,我确实有时间偏移,我希望有一种方法可以将JS中的此字符串转换为日期对象,然后使用偏移显示时间正确的时区。

请注意我要显示的是以下格式:

下午12:19 mt

我知道我可以将绳子解析出来,但我觉得这不是最好的做法,但是我可能错了。

另请注意,我将不得不使用偏置获得时区(Ex。MT等,也可以是MST,EST)。

I am trying to display the current moment in different time zones. I have tried to use native javascript and the moment-js package but it seems you require the time zone name (ex. "America/Toronto") to display the information I want. The problem is that the information I currently have is the timestamp in string format (see below for string format) from the desired timezone, and the city the timestamp belongs to. The problem with using the city to create my tz value is that one of the cities I want to display isn't in the IANA tz database (Calgary).

String timestamp:

2022-04-26T14:19:42.8430964-04:00

As can be seen I do have the time offset and I was hoping there was a way to convert this string in js to a Date object and then display the time using the offset in the correct time zone.

Note what I want to display is the following format:

12:19 pm MT

I know I could just parse out the string but I feel like this isn't the best practice, however I may be wrong.

Also note that I will have to get the time zone (ex. MT, ET it can also be MST, EST) using the offset.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2025-01-31 09:32:13

您不需要这里的时刻。 JS可以在本地做到这一点。 现在也已弃用

您的IANA时区为America/Edmonton。因此,这很简单。该日期格式是ISO 8601日期格式,因此您只需将其传递到new Date构造函数中,它将正确解析它:

const iso8601 = '2022-04-26T14:19:42.8430964-04:00';
const date = new Date(iso8601);
console.log(date.toLocaleString('en-US', { timeZone: 'America/Edmonton' }));

您的输入日期设置了哪个时区,只要它在ISO日期中具有正确的偏移。一旦是date,偏移就无关紧要。例如:

const iso8601 = '2022-04-26T14:19:42.8430964Z';
const date = new Date(iso8601);
//time will now be 4 hours off above as the input date is UTC
console.log(date.toLocaleString('en-US', { timeZone: 'America/Edmonton' }));

you don't need moment here. JS can do this natively. Also moment is now deprecated.

Your IANA timezone is America/Edmonton. So it's simple to do. That date format is an ISO 8601 date format, so you can just pass it into the new Date constructor and it'll parse it correctly:

const iso8601 = '2022-04-26T14:19:42.8430964-04:00';
const date = new Date(iso8601);
console.log(date.toLocaleString('en-US', { timeZone: 'America/Edmonton' }));

It doesn't matter what timezone your input date is set, so long as it has the correct offset in the ISO date. Once it's a Date, the offset is irrelevant. E.g.:

const iso8601 = '2022-04-26T14:19:42.8430964Z';
const date = new Date(iso8601);
//time will now be 4 hours off above as the input date is UTC
console.log(date.toLocaleString('en-US', { timeZone: 'America/Edmonton' }));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文