久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    1. <legend id='NpyIL'><style id='NpyIL'><dir id='NpyIL'><q id='NpyIL'></q></dir></style></legend>
      • <bdo id='NpyIL'></bdo><ul id='NpyIL'></ul>

      <small id='NpyIL'></small><noframes id='NpyIL'>

      <i id='NpyIL'><tr id='NpyIL'><dt id='NpyIL'><q id='NpyIL'><span id='NpyIL'><b id='NpyIL'><form id='NpyIL'><ins id='NpyIL'></ins><ul id='NpyIL'></ul><sub id='NpyIL'></sub></form><legend id='NpyIL'></legend><bdo id='NpyIL'><pre id='NpyIL'><center id='NpyIL'></center></pre></bdo></b><th id='NpyIL'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='NpyIL'><tfoot id='NpyIL'></tfoot><dl id='NpyIL'><fieldset id='NpyIL'></fieldset></dl></div>

      <tfoot id='NpyIL'></tfoot>

      在 JavaScript 中計算給定 TimeZone 字符串的 UTC 偏移

      Calculate the UTC offset given a TimeZone string in JavaScript(在 JavaScript 中計算給定 TimeZone 字符串的 UTC 偏移量)
    2. <legend id='qPj74'><style id='qPj74'><dir id='qPj74'><q id='qPj74'></q></dir></style></legend>
          <tfoot id='qPj74'></tfoot>
          <i id='qPj74'><tr id='qPj74'><dt id='qPj74'><q id='qPj74'><span id='qPj74'><b id='qPj74'><form id='qPj74'><ins id='qPj74'></ins><ul id='qPj74'></ul><sub id='qPj74'></sub></form><legend id='qPj74'></legend><bdo id='qPj74'><pre id='qPj74'><center id='qPj74'></center></pre></bdo></b><th id='qPj74'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='qPj74'><tfoot id='qPj74'></tfoot><dl id='qPj74'><fieldset id='qPj74'></fieldset></dl></div>

              <tbody id='qPj74'></tbody>
              <bdo id='qPj74'></bdo><ul id='qPj74'></ul>
              • <small id='qPj74'></small><noframes id='qPj74'>

                本文介紹了在 JavaScript 中計算給定 TimeZone 字符串的 UTC 偏移量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                使用標準 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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發技術分
                Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)
                Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)

                    1. <legend id='aB0Nu'><style id='aB0Nu'><dir id='aB0Nu'><q id='aB0Nu'></q></dir></style></legend>
                      <tfoot id='aB0Nu'></tfoot>

                        <bdo id='aB0Nu'></bdo><ul id='aB0Nu'></ul>
                            <tbody id='aB0Nu'></tbody>
                        • <i id='aB0Nu'><tr id='aB0Nu'><dt id='aB0Nu'><q id='aB0Nu'><span id='aB0Nu'><b id='aB0Nu'><form id='aB0Nu'><ins id='aB0Nu'></ins><ul id='aB0Nu'></ul><sub id='aB0Nu'></sub></form><legend id='aB0Nu'></legend><bdo id='aB0Nu'><pre id='aB0Nu'><center id='aB0Nu'></center></pre></bdo></b><th id='aB0Nu'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='aB0Nu'><tfoot id='aB0Nu'></tfoot><dl id='aB0Nu'><fieldset id='aB0Nu'></fieldset></dl></div>
                        • <small id='aB0Nu'></small><noframes id='aB0Nu'>

                          主站蜘蛛池模板: 久久高清 | 午夜小视频在线播放 | 蜜臀久久99精品久久久久久宅男 | 国产免费一区二区 | 国产欧美日韩一区二区三区在线观看 | 97人人澡人人爽91综合色 | 久久精品国产清自在天天线 | 韩日在线观看视频 | 久草成人网 | 日韩一区二区在线免费观看 | 求毛片| 女朋友的闺蜜3韩国三级 | 99精品网 | 国产视频福利一区 | 久久精品国产一区二区电影 | 久久久女女女女999久久 | 国产精品久久久久久久久免费软件 | 91久久精品国产91久久性色tv | 伊人精品一区二区三区 | 久久国产一区二区三区 | 在线一级片 | 精品国产乱码久久久久久果冻传媒 | 四虎影院新地址 | 欧美v免费 | 伊人影院99 | 日韩在线日韩 | 欧美一区二区大片 | 激情六月丁香婷婷 | 亚洲国产一区在线 | 久久久久国产一级毛片高清网站 | 欧美不卡在线 | 欧美不卡在线 | 一级片免费视频 | 视频在线亚洲 | 久草精品视频 | 久久精品欧美一区二区三区不卡 | 国产精品91久久久久久 | 污视频免费在线观看 | 中文字幕一区二区三区在线观看 | 亚洲在线一区二区三区 | 日日摸日日添日日躁av |