pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    • <bdo id='Dnops'></bdo><ul id='Dnops'></ul>

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

    <legend id='Dnops'><style id='Dnops'><dir id='Dnops'><q id='Dnops'></q></dir></style></legend>
  • <tfoot id='Dnops'></tfoot>

      1. <i id='Dnops'><tr id='Dnops'><dt id='Dnops'><q id='Dnops'><span id='Dnops'><b id='Dnops'><form id='Dnops'><ins id='Dnops'></ins><ul id='Dnops'></ul><sub id='Dnops'></sub></form><legend id='Dnops'></legend><bdo id='Dnops'><pre id='Dnops'><center id='Dnops'></center></pre></bdo></b><th id='Dnops'></th></span></q></dt></tr></i><div class="wu2ay20" id='Dnops'><tfoot id='Dnops'></tfoot><dl id='Dnops'><fieldset id='Dnops'></fieldset></dl></div>
      2. 如何在 JavaScript 中計算 2 個時區的差異?

        How Do I calculate the difference of 2 time zones in JavaScript?(如何在 JavaScript 中計算 2 個時區的差異?)

      3. <tfoot id='Iktbi'></tfoot>
          <tbody id='Iktbi'></tbody>

          <bdo id='Iktbi'></bdo><ul id='Iktbi'></ul>
          <legend id='Iktbi'><style id='Iktbi'><dir id='Iktbi'><q id='Iktbi'></q></dir></style></legend>

            • <small id='Iktbi'></small><noframes id='Iktbi'>

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

                  本文介紹了如何在 JavaScript 中計算 2 個時區的差異?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  例如,東部和中部的區別是 1.我下面的解決方案感覺很hacky.有沒有更簡單/更好的方法?

                  For example, the difference in Eastern and Central is 1. My solution below feels hacky. Is there a easier / better way?

                  var diff = (parseInt(moment().tz("America/New_York").format("ZZ")) - parseInt(moment().tz("America/Chicago").format("ZZ"))) / 100;
                  

                  我的示例是使用 Momentjs 庫.

                  My example is using the Momentjs library.

                  推薦答案

                  不可能計算兩個任意時區之間的差異.您只能計算特定時刻的差異.

                  It's impossible to calculate the difference between two arbitrary time zones. You can only calculate a difference for a specific moment in time.

                  • 目前倫敦和紐約之間的時差為 4 小時(寫于 2015 年 3 月 25 日).
                  • 但幾周前相差 5 小時,而幾周后將是 5 小時.
                  • 每個時區在不同時間點切換夏令時偏移量.
                  • It's currently 4 hours difference between London and New York (writing this on Mar 25, 2015).
                  • But it was 5 hours difference a few weeks ago, and it will be 5 a few weeks from now.
                  • Each time zone switches offsets for daylight saving time at a different point in time.

                  這在兩個時區之間的一般情況下是正確的.然而一些時區要么完全同時切換,要么根本不切換.

                  This is true in the general case between two time zones. However some time zones either switch exactly at the same time, or don't switch at all.

                  • 倫敦和巴黎之間總是一小時,因為它們在同一時間切換.
                  • 亞利桑那州和夏威夷之間總是 3 小時,因為兩者都沒有夏令時.
                  • There is always one hour between London and Paris, because they switch at the same moment in time.
                  • There are always 3 hours between Arizona and Hawaii, because neither have DST.

                  請注意,在美國,使用 DST 的每個時區實際上會在不同的時間切換.它們都在其本地時間的凌晨 2:00 切換,但不是在同一時間通用.

                  Keep in mind that in the United States, each time zone that uses DST actually switches at a different moment in time. They all switch at 2:00 AM in their local time, but not at the same universal moment in time.

                  • 所以在芝加哥和紐約之間,通常相隔 1 小時
                  • 但每年有兩個短暫的時期,它們要么相隔 2 小時,要么具有相同的確切時間.
                  • So between Chicago and New York, there is usually 1 hour apart
                  • But for two brief periods each year they are either 2 hours apart, or have the same exact time.

                  另請參閱時區標簽wiki中的時區!=偏移".

                  See also "Time Zone != Offset" in the timezone tag wiki.

                  現在關于時刻時區,您在評論中說:

                  Now with regard to moment-timezone, you said in comments:

                  網絡服務器位于東部時區;我們在中環.我需要用戶時區和服務器的差異.

                  Web 服務器的時區無關緊要.您應該能夠在世界任何地方進行托管,而不會影響您的應用程序.如果你不能,那么你做錯了.

                  The time zone of the web server is irrelevant. You should be able to host from anywhere in the world without affecting your application. If you can't, then you're doing it wrong.

                  可以獲取您的時區(美國中部時間)與用戶的時區之間的當前時差.如果代碼在瀏覽器中運行,您甚至不需要知道用戶的確切時區:

                  You can get the current time difference between your time zone (US Central time) and the user's. You don't even need to know the user's exact time zone for this, if the code is running in the browser:

                  var now = moment();
                  var localOffset = now.utcOffset();
                  now.tz("America/Chicago"); // your time zone, not necessarily the server's
                  var centralOffset = now.utcOffset();
                  var diffInMinutes = localOffset - centralOffset;
                  

                  如果代碼在服務器上運行(在 node.js 應用程序中),那么您需要知道用戶的時區.只需像這樣更改第一行:

                  If instead the code was running on the server (in a node.js app), then you would need to know the user's time zone. Just change the first line like this:

                  var now = moment.tz("America/New_York"); // their time zone
                  

                  更新答案:

                  這可以在支持 ECMAScript 國際化 API 并已完全實現 IANA 時區支持的環境中完成.這是當今大多數瀏覽器.

                  Updated answer:

                  This can be done without Moment, in environments that support the ECMAScript Internationalization API and have fully implemented IANA time zone support. This is most browsers these days.

                  function getTimeZoneOffset(date, timeZone) {
                  
                    // Abuse the Intl API to get a local ISO 8601 string for a given time zone.
                    let iso = date.toLocaleString('en-CA', { timeZone, hour12: false }).replace(', ', 'T');
                  
                    // Include the milliseconds from the original timestamp
                    iso += '.' + date.getMilliseconds().toString().padStart(3, '0');
                  
                    // Lie to the Date object constructor that it's a UTC time.
                    const lie = new Date(iso + 'Z');
                  
                    // Return the difference in timestamps, as minutes
                    // Positive values are West of GMT, opposite of ISO 8601
                    // this matches the output of `Date.getTimeZoneOffset`
                    return -(lie - date) / 60 / 1000;
                  }
                  

                  示例用法:

                  getTimeZoneOffset(new Date(2020, 3, 13), 'America/New_York') //=> 240
                  getTimeZoneOffset(new Date(2020, 3, 13), 'Asia/Shanghai') //=> -480
                  

                  如果你想要它們之間的差異,你可以簡單地減去結果.

                  If you want the difference between them, you can simply subtract the results.

                  上述函數在 full-icu 國際化 支持已安裝(這是 Node 13 和更新版本的默認設置).如果您有帶有 system-icusmall-icu 的舊版本,則可以使用此修改后的功能.它也可以在瀏覽器和 full-icu 環境中工作,但更大一些.(我在 Linux 上的 Node 8.17.0 和 Windows 上的 Node 12.13.1 上對此進行了測試.)

                  The above function works in Node.js where the full-icu internationalization support is installed (which is the default for Node 13 and newer). If you have an older version with either system-icu or small-icu, you can use this modified function. It will work in browsers and full-icu environments also, but is a bit larger. (I have tested this on Node 8.17.0 on Linux, and Node 12.13.1 on Windows.)

                  function getTimeZoneOffset(date, timeZone) {
                  
                    // Abuse the Intl API to get a local ISO 8601 string for a given time zone.
                    const options = {timeZone, calendar: 'iso8601', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false};
                    const dateTimeFormat = new Intl.DateTimeFormat(undefined, options);
                    const parts = dateTimeFormat.formatToParts(date);
                    const map = new Map(parts.map(x => [x.type, x.value]));
                    const year = map.get('year');
                    const month = map.get('month');
                    const day = map.get('day');
                    const hour = map.get('hour');
                    const minute = map.get('minute');
                    const second = map.get('second');
                    const ms = date.getMilliseconds().toString().padStart(3, '0');
                    const iso = `${year}-${month}-${day}T${hour}:${minute}:${second}.${ms}`;
                  
                    // Lie to the Date object constructor that it's a UTC time.
                    const lie = new Date(iso + 'Z');
                  
                    // Return the difference in timestamps, as minutes
                    // Positive values are West of GMT, opposite of ISO 8601
                    // this matches the output of `Date.getTimeZoneOffset`
                    return -(lie - date) / 60 / 1000;
                  }
                  

                  請注意,無論哪種方式,我們必須通過 Intl 才能正確應用時區.

                  Note that either way, we must go through Intl to have the time zone applied properly.

                  這篇關于如何在 JavaScript 中計算 2 個時區的差異?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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() 的限制?)

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

                  • <bdo id='SYHsr'></bdo><ul id='SYHsr'></ul>

                        <tfoot id='SYHsr'></tfoot>
                        • <legend id='SYHsr'><style id='SYHsr'><dir id='SYHsr'><q id='SYHsr'></q></dir></style></legend>

                              <tbody id='SYHsr'></tbody>

                          1. <i id='SYHsr'><tr id='SYHsr'><dt id='SYHsr'><q id='SYHsr'><span id='SYHsr'><b id='SYHsr'><form id='SYHsr'><ins id='SYHsr'></ins><ul id='SYHsr'></ul><sub id='SYHsr'></sub></form><legend id='SYHsr'></legend><bdo id='SYHsr'><pre id='SYHsr'><center id='SYHsr'></center></pre></bdo></b><th id='SYHsr'></th></span></q></dt></tr></i><div class="kuyoikw" id='SYHsr'><tfoot id='SYHsr'></tfoot><dl id='SYHsr'><fieldset id='SYHsr'></fieldset></dl></div>
                            主站蜘蛛池模板: 金属雕花板_厂家直销_价格低-山东慧诚建筑材料有限公司 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 上海恒驭仪器有限公司-实验室平板硫化机-小型平板硫化机-全自动平板硫化机 | 电子万能试验机_液压拉力试验机_冲击疲劳试验机_材料试验机厂家-济南众标仪器设备有限公司 | 自动化生产线-自动化装配线-直流电机自动化生产线-东莞市慧百自动化有限公司 | PVC地板|PVC塑胶地板|PVC地板厂家|地板胶|防静电地板-无锡腾方装饰材料有限公司-咨询热线:4008-798-128 | IPO咨询公司-IPO上市服务-细分市场研究-龙马咨询 | 苏州工作服定做-工作服定制-工作服厂家网站-尺品服饰科技(苏州)有限公司 | 北京京云律师事务所 | 铝箔-铝板-花纹铝板-铝型材-铝棒管-上海百亚金属材料有限公司 | 屏蔽服(500kv-超高压-特高压-电磁)-徐吉电气 | 智能门锁电机_智能门锁离合器_智能门锁电机厂家-温州劲力智能科技有限公司 | 中红外QCL激光器-其他连续-半导体连续激光器-筱晓光子 | 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 内六角扳手「厂家」-温州市威豪五金工具有限公司 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 双舌接地线-PC68数字式高阻计-ZC36|苏海百科| 衬氟旋塞阀-卡套旋塞阀-中升阀门首页 | 成都治疗尖锐湿疣比较好的医院-成都治疗尖锐湿疣那家医院好-成都西南皮肤病医院 | 杭州|上海贴标机-百科 | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 | 南京租车,南京汽车租赁,南京包车,南京会议租车-南京七熹租车 | 热处理炉-退火炉-回火炉设备厂家-丹阳市电炉厂有限公司 | 滚筒线,链板线,总装线,流水线-上海体能机电有限公司 | 沟盖板_复合沟盖板厂_电力盖板_树脂雨水篦子-淄博拜斯特 | 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 单级/双级旋片式真空泵厂家,2xz旋片真空泵-浙江台州求精真空泵有限公司 | 压缩空气冷冻式干燥机_吸附式干燥机_吸干机_沪盛冷干机 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 定坤静电科技静电消除器厂家-除静电设备 | 苏州柯瑞德货架-仓库自动化改造解决方案| 润滑脂-高温润滑脂-轴承润滑脂-食品级润滑油-索科润滑油脂厂家 | 骨灰存放架|骨灰盒寄存架|骨灰架厂家|智慧殡葬|公墓陵园管理系统|网上祭奠|告别厅智能化-厦门慈愿科技 | 扒渣机,铁水扒渣机,钢水扒渣机,铁水捞渣机,钢水捞渣机-烟台盛利达工程技术有限公司 | 层流手术室净化装修-检验科ICU改造施工-华锐净化工程-特殊科室建设厂家 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 超声波分散机-均质机-萃取仪-超声波涂料分散设备-杭州精浩 | 润东方环保空调,冷风机,厂房车间降温设备-20年深圳环保空调生产厂家 | 比士亚-专业恒温恒湿酒窖,酒柜,雪茄柜的设计定制 | 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 老城街小面官网_正宗重庆小面加盟技术培训_特色面馆加盟|牛肉拉面|招商加盟代理费用多少钱 |