【问题标题】:Get all possible timezones from UTC offset从 UTC 偏移量获取所有可能的时区
【发布时间】:2018-05-31 07:05:57
【问题描述】:

我有以下值 -
GMT -0800
如何获取共享此偏移量的所有时区列表?

【问题讨论】:

    标签: javascript node.js datetime momentjs


    【解决方案1】:

    这是一个按偏移量列出时区的示例,值得指出的是,由于许多区域的夏令时,给定时区的 UTC 偏移量会发生变化。

    <html>
    <head>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.js"></script>
    <script src="https://momentjs.com/downloads/moment.js"></script>
    <script src="https://momentjs.com/downloads/moment-timezone-with-data-2012-2022.js"></script>
    
    <script>
    
    
    function initialise() {
    
        $("#offsetSelector").on('change', function (event) {
           showTimeZones();
        });
    
        var startOffset = -12;
        var offsets = Array.from(Array(48).keys()).reduce((acc, val) => {
            acc.push(startOffset);
            startOffset += 0.5;
            return acc;
        }, []);
    
        offsets.forEach((offset) => {
            var selector = document.getElementById("offsetSelector");
            var el = document.createElement("option");
            el.textContent = offset;
            el.value = offset;
            selector.appendChild(el);
        });
    
        document.getElementById("offsetSelector").value = -8;
        showTimeZones();
    }
    
    function showTimeZones() {
    
        var utcOffset = $('#offsetSelector').val();
        var timeZones = moment.tz.names();
    
        var result = timeZones.filter((zone) => {
            var tz = moment.tz.zone(zone);
            /* We'll make the assumption we're looking for the offset outside of DST */
            var currentOffset = tz.utcOffset(new Date('2018-01-01'));
            return  (currentOffset === (-utcOffset*60));
        });
    
        $("#list").empty();
        console.log('Zones:');
        result.forEach((zoneName) => {
          console.log(zoneName);
          var ul = document.getElementById("list");
          var li = document.createElement("li");
          li.innerHTML = zoneName;
         // li.appendChild(document.createTextNode(zoneName));
          ul.appendChild(li);
        });
    }
    
    </script>
    
    </head>
    <body onLoad = "initialise()">
    <b>Offset (hours):</b>
    <select id="offsetSelector">
    </select> 
    
    <br/><br/><b>TimeZone list:</b><br/>
    <ul id="list"></ul>
    </body>
    </html>
    

    JSFiddle: https://jsfiddle.net/p9h5wgcr/

    【讨论】:

      【解决方案2】:

      我将分两部分回答:

      1) 查找包含时区名称及其偏移量的数据集

      您可以手动编译此数据集(可能使用来自 Wikipedia 的数据),或者 NPM 包 timezones.json 看起来很合适。

      2) 搜索列表

      给定两个输入:初始时区和偏移量(例如 GMT-8),您需要:

      1. 在您的数据集中搜索初始时区的偏移量(例如,GMT+0

      2. 将两个值相加,例如GMT-8 => 0 - 8 = -8

      3. 过滤数据集以获取偏移量为-8的值

      示例:

      假设您在变量 timezones_file 中拥有来自上述 NPM 包的 timezones.json 的内容

      const timezones = JSON.parse(timezones_file);
      
      // Step 1 above
      function findOffset(timezone) {
         const matches = timezones.filter( zone => zone.abbr === timezone );
         return matches ? matches[0].offset : null;
      }
      
      // Step 2 above
      function findByOffset(offset) {
          return timezones.filter( zone => zone.offset === offset );
      }
      
      // Answer to your question, accepts a timezone (e.g. 'GMT') and offset (e.g. -8)
      function getListOfTimezones(initial_timezone, initial_offset) {
          const new_offset = findOffset(initial_timezone) + initial_offset;
          return findByOffset(new_offset);
      }
      

      【讨论】:

        猜你喜欢
        • 2016-05-07
        • 2013-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-29
        • 2014-01-09
        • 2015-06-16
        • 1970-01-01
        相关资源
        最近更新 更多