在Javascript中从时区获取utc偏移量 [英] Get utc offset from timezone in Javascript
问题描述
我需要一个给出时区的Javascript函数,返回当前的utc偏移量。
I need a Javascript function that given a timezone, returns the current utc offset.
例如,theFuncIneed('US / Eastern') - > 240
For example, theFuncIneed('US/Eastern') -> 240
任何想法?
谢谢
推荐答案
一般情况下,这是不可能的。
-
US / Eastern
是时区的标识符。 (它实际上是America / New_York
的别名,这是真正的标识符。)
US/Eastern
is an identifier for a time zone. (It's actually an alias toAmerica/New_York
, which is the real identifier.)
240
是时区 offset 。它通常写成 -04:00
(反转符号,除以60)。
240
is a time zone offset. It's more commonly written as -04:00
(Invert the sign, divide by 60).
美国东部时区由东部标准时间组成,其偏移量 -05:00
和东部夏令时间,偏移量 -04:00
。
The US Eastern Time Zone is comprised of both Eastern Standard Time, which has the offset of -05:00
and Eastern Daylight Time, which has the offset of -04:00
.
所以说不准确美国/东部
= 240.请阅读时区标签wiki ,特别是标题为时区!=偏移的部分。
So it is not at all accurate to say US/Eastern
= 240. Please read the timezone tag wiki, especially the section titled "Time Zone != Offset".
现在您确实要求当前偏移量,其中是可能。如果您提供日期+时间参考,则可以解决此问题。
Now you did ask for the current offset, which is possible. If you supply a date+time reference, then you can resolve this.
-
对于计算机的本地时区javascript代码正在执行,它内置于
.getTimeZoneOffset()
,来自Date
对象的任何实例。
For the local time zone of the computer where the javascript code is executing, this is built in with
.getTimeZoneOffset()
from any instance of aDate
object.
但是如果你想要它用于特定的时区,那么你需要使用我在此列出的其中一个库。
But if you want it for a specific time zone, then you will need to use one of the libraries I listed here.
这篇关于在Javascript中从时区获取utc偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!