問題描述
使用標準 JS 庫 (ECMA5),不使用 momentjs 或外部庫,如何計算給定 TimeZone 字符串(例如Europe/Rome"或America/Los_Angeles")的 UTC 偏移量?
UTC 偏移量可能取決于它是否是 DST,因此如果解決方案需要將本地客戶端日期轉換為指定的時區字符串,這將是有意義的.目標只是知道與 UTC 的偏移量.
函數 getUtcOffset(timezone) {//返回整數值.//>0 如果 +GMT//<如果 -GMT 則為 0.}
ECMAScript中沒有函數(ECMA-262) 可以執行您請求的操作.這僅僅是因為標準的 ECMAScript 除了本地計算機和 UTC 之外,對時區一無所知.
但是,在支持 ECMAScript 國際化 API (ECMA-402),并且完全支持 IANA 時區數據庫標識符,您可以將這樣的函數組合在一起:
function getTimezoneOffset(d, tz) {const a = d.toLocaleString("ja", {timeZone: tz}).split(/[/s:]/);一個[1]——;const t1 = Date.UTC.apply(null, a);const t2 = new Date(d).setMilliseconds(0);返回 (t2 - t1)/60/1000;}
這將適用于當前版本的 Chrome,也許在其他一些地方.但肯定不能保證在任何地方都能工作.特別是,它不能在任何版本的 Internet Explorer 瀏覽器中運行.
示例用法(在 Chrome 中):
getTimezoneOffset(new Date(2016, 0, 1), "America/New_York")//300getTimezoneOffset(new Date(2016, 6, 1), America/New_York")//240getTimezoneOffset(new Date(2016, 0, 1), "Europe/Paris")//-60getTimezoneOffset(new Date(2016, 6, 1), "Europe/Paris")//-120
關于這個特定功能的一些注意事項:
就像我提到的,它不會在任何地方都有效.最終,所有瀏覽器都會趕上現代標準,但目前還不會.
你傳入的日期確實會影響結果.這是由于夏令時和其他時區異常.您可以只使用
new Date()
傳遞 current 日期,但結果會根據您調用函數的時間而改變.請參閱時區!= 偏移量";在時區標簽wiki中.此函數的結果與
Date.getTimezoneOffset
相同 - 以分鐘為單位,正值是 UTC 的 West.如果您使用 ISO8601 偏移量,則需要轉換為小時數并反轉符號.該函數依賴于
toLocaleString
函數的時區格式化功能.我選擇了'ja'
文化,因為日期部分已經按照數組的正確順序排列.這確實是一個黑客.理想情況下,會有一個 API 可以讓您訪問時區信息,而無需在格式化時將其綁定到語言環境.不幸的是,這個特定 API 的設計者犯了將時區與語言環境相關聯的錯誤.這是在其他一些不同語言的 API 中犯的錯誤,不幸的是在這里被帶到了 JavaScript 中.明確重申:ECMA-402 中唯一的時區功能是在格式化字符串時應用時區,恕我直言,這是一個設計缺陷.
在我上面的示例使用部分中有一個錯誤,這說明了為什么這個 API 是錯誤的部分原因.具體來說,當您創建
Date
對象時,無法指定時區.我傳入的 1 月 1 日和 7 月 1 日日期是在 local 時區創建的,而不是在指定的時區.因此,輸出可能與您在轉換附近的預期不完全一致.這可能會被更多地破解來解決這個問題,但我會把它作為練習留給你.
再次 - 雖然這個答案滿足要求的標準,因為沒有涉及外部庫,我強烈建議 反對 在任何生產代碼中使用它.如果您打算為此做任何重要的事情,我會使用我在此處列出的庫之一.p>
Using the standard JS library (ECMA5), without using momentjs or external libs, how do you calculate the UTC offset given a TimeZone string such as "Europe/Rome" or "America/Los_Angeles"?
UTC Offsets may depend on whether it is DST or not, so it would make sense if the solution required converting the local client date to the specified Timezone String. The goal is just to know the offset from UTC.
function getUtcOffset(timezone) {
// return int value.
// > 0 if +GMT
// < 0 if -GMT.
}
There is no function in ECMAScript (ECMA-262) that can perform the operation you requested. This is simply because standard ECMAScript does not know anything about time zones other than that of the local computer, and UTC.
However, in browsers that support the ECMAScript Internationalization API (ECMA-402), and fully support the IANA time zone database identifiers, you can hack together a function like this:
function getTimezoneOffset(d, tz) {
const a = d.toLocaleString("ja", {timeZone: tz}).split(/[/s:]/);
a[1]--;
const t1 = Date.UTC.apply(null, a);
const t2 = new Date(d).setMilliseconds(0);
return (t2 - t1) / 60 / 1000;
}
This will work in current versions of Chrome, and perhaps in a few other places. but it is certainly not guaranteed to work everywhere. In particular, it won't work in Internet Explorer browsers of any version.
Example usage (in Chrome):
getTimezoneOffset(new Date(2016, 0, 1), "America/New_York") // 300
getTimezoneOffset(new Date(2016, 6, 1), "America/New_York") // 240
getTimezoneOffset(new Date(2016, 0, 1), "Europe/Paris") // -60
getTimezoneOffset(new Date(2016, 6, 1), "Europe/Paris") // -120
A few things to note about this particular function:
Like I mentioned, It's not going to work everywhere. Eventually, as all browsers catch up to modern standards it will, but it won't currently.
The date you pass in will indeed affect the result. This is due daylight saving time and other time zone anomalies. You can pass the current date just with
new Date()
, but the result will change based on when you call the function. See "time zone != offset" in the timezone tag wiki.The results of this function are the same as
Date.getTimezoneOffset
- in terms of minutes, with positive values being West of UTC. If you are working with ISO8601 offsets, you'll need to convert to hours and invert the sign.The function relies on the time zone formatting functionality of the
toLocaleString
function. I picked the'ja'
culture, because the date parts were already in the correct order for the array. This is a hack indeed. Ideally there would be an API that would let you access time zone information without binding it to a locale when formatting. Unfortunately, the designers of this particular API have made the mistake of associating time zone with locale. This is a mistake that's been made in a few other APIs from various languages, and unfortunately was carried into JavaScript here.Restated plainly: The only time zone functionality in ECMA-402 is to apply a time zone when formatting a string, which is a design flaw, IMHO.
There's a bug in my example usage section above, that exemplifies part of why this API is a mistake. Specifically, there's no way to specify a time zone when you create a
Date
object. The Jan 1st and July 1st dates I pass in are created in the local time zone, not in the time zone specified. Therefore, the output may not be exactly what you expect near a transition. This could be hacked even more to work around this problem, but I will leave that as an exercise to you.
Again - Though this answer satisfies the criteria asked for, as there are no external libraries involved, I strongly recommend against using this in any production code. If you're planning on doing anything important with this, I'd use one of the libraries I listed here.
這篇關于在 JavaScript 中計算給定 TimeZone 字符串的 UTC 偏移量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!