你可以通过偏移获取时区名称吗?

huangapple go评论50阅读模式
英文:

Can I get timeZone name by offset?

问题

我想获取时区ID列表(例如:America/Los_Angeles),在这些时区中,时间是上午10点。

因此,如果UTC时间是凌晨1点,我将获取所有GMT +9或偏移量为10 * 60 * 60的时区。

我可以如何在moment.js中实现这个?

英文:

I want to get the list of timeZoneIds (like: America/Los_Angeles) that at there, the time is 10am

So if UTC time is 1am, I will get all timezones of GMT +9 or offset = 10 * 60 * 60

How I can do it with moment.js ?

答案1

得分: 1

now = moment()
获取当前时间

zones_at_10 = moment.tz.names().filter(zone => now.tz(zone).hour() == 10)
这将获取当前小时为10的所有时区(包括当前时间为10:01以及当前时间为10:31的时区)。如果您只想限制为当前时间为10:00的时区,可以改用以下代码:

zones_at_10 = moment.tz.names().filter(zone => now.tz(zone).format("H:s") == "10:00")
这将获取当前时间为10:00的所有时区。

英文:
now = moment()
zones_at_10 = moment.tz.names().filter(zone => now.tz(zone).hour() == 10)

This will get all zones where the hour is currently 10 (including those where it is 10:01 as well as those where it is 10:31). If you wish to restrict to only the zones where it is currently 10:00, you can use this instead:

zones_at_10 = moment.tz.names().filter(zone => now.tz(zone).format("H:s") == "10:00")

huangapple
  • 本文由 发表于 2023年7月10日 15:11:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定