简体   繁体   English

使用时区偏移获取时区名称/列表

[英]Get timezone name/list using timezone offset

I am having the time zone offset using the below code.我使用以下代码进行时区偏移。 I want to find out the list of timezone names associated with the timezone offset.我想找出与时区偏移关联的时区名称列表。

new Date().getTimezoneOffset().toString()新日期().getTimezoneOffset().toString()

I think the you have to use moment-timezone.js library for this. 我认为您必须为此使用moment-timezone.js库。

(Link here: https://momentjs.com/timezone/ ) (链接在这里: https : //momentjs.com/timezone/

The approach should go along this lines: 该方法应遵循以下原则:

  1. import the library (Note: this library is dependent on moment.js library - import that before) 导入库(注意:该库依赖moment.js库-之前导入)
  2. use moment.tz.names() function to get all the available timezone locations. 使用moment.tz.names()函数获取所有可用的时区位置。
  3. use moment.tz.zone(name) function to get the zone object 使用moment.tz.zone(name)函数获取区域对象
  4. use the offsets property from the zone object to get the location offset 使用区域对象offsets属性获取位置偏移量
  5. create a map to hold the same offset names. 创建一个地图以容纳相同的偏移量名称。
  6. loop through the offsets (one location can share multiple offsets) and add them to the map as well as the name of location to each key. 遍历偏移量(一个位置可以共享多个偏移量)并将其添加到地图以及每个键的位置名称。
  7. access that map with specific offset and get the list of timezones. 访问具有特定偏移量的地图,并获取时区列表。

The code will look something like this: 该代码将如下所示:

 const tzNames = moment.tz.names(); const map = new Map(); for (const name of tzNames) { const offsets = moment.tz.zone(name).offsets; for (const offset of offsets) { if (!map.has(offset)) { map.set(offset, new Set()); } map.get(offset).add(name); } } const currentOffset = new Date().getTimezoneOffset(); const offsetList = map.get(currentOffset); console.log('currentOffset: ' + currentOffset); console.log('offset list size: ' + offsetList.size); console.log('Total different offsets: ' + map.size); console.log('List items: '); for (const item of offsetList) { console.log(item); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script> 

You can do it in two steps in vanilla js using intl object, local "ia" (see https://stackoverflow.com/a/64262840/1061871 ) and Intl.supportedValuesOf('timeZone') ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/supportedValuesOf ):您可以使用intl object、本地“ia”(参见https://stackoverflow.com/a/64262840/1061871 )和Intl.supportedValuesOf('timeZone')https://developer. mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/supportedValuesOf ):

const getOffset = (tz) => Intl.DateTimeFormat("ia", {
                timeZoneName: "shortOffset",
                timeZone: tz
              }) 
                .formatToParts()
                .find((i) => i.type === "timeZoneName").value // => "GMT+/-OFFSET in hh:mm"
                .slice(3); //remove the GMT to get the offset in hh:mm 

const getTZList = (offset) =>

     Intl.supportedValuesOf('timeZone').filter(tz => getOffset(tz) === offset)


console.log(getTZList("-3:30"), getTZList("")) //for list of timezone in gmt/utc use empty string, not 0

edit, forgot: date object to offset in +/-hh:mm format编辑,忘记了:日期 object 以 +/-hh:mm 格式偏移

const getUtcOffset = (dateObject = new Date()) => {
    
    const offset = dateObject.getTimezoneOffset(), o = Math.abs(offset);
    return  (offset === 0 ?  '' : ((offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2)));
 }
 console.log(getTZList(getUtcOffset(new Date()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM