問題描述
我需要一個給定時區的 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
是一個時區偏移量.它通常寫成 -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
.
所以說 US/Eastern
= 240 一點也不準確.請閱讀 timezone 標簽wiki,尤其是標題為Time Zone != Offset"的部分.
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偏移量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!