如何使用偏移值在不同时区显示时间
我试图在不同时区显示当前时刻。我尝试使用本机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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要这里的时刻。 JS可以在本地做到这一点。 现在也已弃用。
您的IANA时区为
America/Edmonton
。因此,这很简单。该日期格式是ISO 8601日期格式,因此您只需将其传递到new Date
构造函数中,它将正确解析它:您的输入日期设置了哪个时区,只要它在ISO日期中具有正确的偏移。一旦是
date
,偏移就无关紧要。例如: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 thenew Date
constructor and it'll parse it correctly: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.: