如何使用JavaScript中的时区名称和地区在UTC中查找时区偏移

时间:2019-06-05 10:29:55

标签: javascript datetime timezone timezone-offset

我正在从 Google Microsoft 获取用户的日历时区。 Google在其日历API 响应中提供了时区区域: timezone: Asia/Kolkata

Microsoft Graph API 提供了类似timezone: 'Indian Standard Time'的时区详细信息 我想在几秒钟内找到时区offset,以便对日期对象执行一些操作。

是否有其他方法或某些来源,我可以以 csv json 的形式获取所有这些详细信息,这些信息可以导入数据库表中并可以用于进一步的操作?

PS:我看了moment.js中的一些示例,但没有发现对我的用例有用

2 个答案:

答案 0 :(得分:4)

我们在这里真正要做的是在Windows和IANA时区名称之间进行映射。

要进行转换,您可以尝试使用Moment Timezone,它使用tz database时区列表支持一系列时区操作。

在此处查看时区的完整列表: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

您可以通过以下网址访问源时区数据库:https://www.iana.org/time-zones

要获取Windows和IANA时区之间的映射,请参阅: https://github.com/unicode-org/cldr/blob/master/common/supplemental/windowsZones.xml

有关JSON格式的相同数据,请参见此处: https://github.com/unicode-cldr/cldr-core/blob/master/supplemental/windowsZones.json

// Timezone objects (Subset from https://github.com/rubenillodo/windows-iana/blob/master/windowsZones.json)
let timezones = [{"windowsName":"Dateline Standard Time","iana":["Etc/GMT+12"]},{"windowsName":"UTC-11","iana":["Etc/GMT+11"]},{"windowsName":"Aleutian Standard Time","iana":["America/Adak"]},{"windowsName":"Hawaiian Standard Time","iana":["Pacific/Honolulu"]},{"windowsName":"Marquesas Standard Time","iana":["Pacific/Marquesas"]},{"windowsName":"Alaskan Standard Time","iana":["America/Anchorage"]},{"windowsName":"UTC-09","iana":["Etc/GMT+9"]},{"windowsName":"Pacific Standard Time (Mexico)","iana":["America/Tijuana"]},{"windowsName":"UTC-08","iana":["Etc/GMT+8"]},{"windowsName":"Pacific Standard Time","iana":["America/Los_Angeles"]},{"windowsName":"US Mountain Standard Time","iana":["America/Phoenix"]},{"windowsName":"Mountain Standard Time (Mexico)","iana":["America/Chihuahua"]},{"windowsName":"Mountain Standard Time","iana":["America/Denver"]},{"windowsName":"Central America Standard Time","iana":["America/Guatemala"]},{"windowsName":"Central Standard Time","iana":["America/Chicago"]},{"windowsName":"Easter Island Standard Time","iana":["Pacific/Easter"]},{"windowsName":"Central Standard Time (Mexico)","iana":["America/Mexico_City"]},{"windowsName":"Canada Central Standard Time","iana":["America/Regina"]},{"windowsName":"SA Pacific Standard Time","iana":["America/Bogota"]},{"windowsName":"Eastern Standard Time (Mexico)","iana":["America/Cancun"]},{"windowsName":"Eastern Standard Time","iana":["America/New_York"]},{"windowsName":"Haiti Standard Time","iana":["America/Port-au-Prince"]},{"windowsName":"Cuba Standard Time","iana":["America/Havana"]},{"windowsName":"US Eastern Standard Time","iana":["America/Indianapolis"]},{"windowsName":"Paraguay Standard Time","iana":["America/Asuncion"]},{"windowsName":"Atlantic Standard Time","iana":["America/Halifax"]},{"windowsName":"Venezuela Standard Time","iana":["America/Caracas"]},{"windowsName":"Central Brazilian Standard Time","iana":["America/Cuiaba"]},{"windowsName":"SA Western Standard Time","iana":["America/La_Paz"]},{"windowsName":"Pacific SA Standard Time","iana":["America/Santiago"]},{"windowsName":"Turks And Caicos Standard Time","iana":["America/Grand_Turk"]},{"windowsName":"Newfoundland Standard Time","iana":["America/St_Johns"]},{"windowsName":"Tocantins Standard Time","iana":["America/Araguaina"]},{"windowsName":"E. South America Standard Time","iana":["America/Sao_Paulo"]},{"windowsName":"SA Eastern Standard Time","iana":["America/Cayenne"]},{"windowsName":"Argentina Standard Time","iana":["America/Buenos_Aires"]},{"windowsName":"Greenland Standard Time","iana":["America/Godthab"]},{"windowsName":"Montevideo Standard Time","iana":["America/Montevideo"]},{"windowsName":"Magallanes Standard Time","iana":["America/Punta_Arenas"]},{"windowsName":"Saint Pierre Standard Time","iana":["America/Miquelon"]},{"windowsName":"Bahia Standard Time","iana":["America/Bahia"]},{"windowsName":"UTC-02","iana":["Etc/GMT+2"]},{"windowsName":"Azores Standard Time","iana":["Atlantic/Azores"]},{"windowsName":"Cape Verde Standard Time","iana":["Atlantic/Cape_Verde"]},{"windowsName":"UTC","iana":["Etc/GMT"]},{"windowsName":"GMT Standard Time","iana":["Europe/London"]},{"windowsName":"Greenwich Standard Time","iana":["Atlantic/Reykjavik"]},{"windowsName":"W. Europe Standard Time","iana":["Europe/Berlin"]},{"windowsName":"Central Europe Standard Time","iana":["Europe/Budapest"]},{"windowsName":"Romance Standard Time","iana":["Europe/Paris"]},{"windowsName":"Morocco Standard Time","iana":["Africa/Casablanca"]},{"windowsName":"Sao Tome Standard Time","iana":["Africa/Sao_Tome"]},{"windowsName":"Central European Standard Time","iana":["Europe/Warsaw"]},{"windowsName":"W. Central Africa Standard Time","iana":["Africa/Lagos"]},{"windowsName":"Jordan Standard Time","iana":["Asia/Amman"]},{"windowsName":"GTB Standard Time","iana":["Europe/Bucharest"]},{"windowsName":"Middle East Standard Time","iana":["Asia/Beirut"]},{"windowsName":"Egypt Standard Time","iana":["Africa/Cairo"]},{"windowsName":"E. Europe Standard Time","iana":["Europe/Chisinau"]},{"windowsName":"Syria Standard Time","iana":["Asia/Damascus"]},{"windowsName":"West Bank Standard Time","iana":["Asia/Hebron"]},{"windowsName":"South Africa Standard Time","iana":["Africa/Johannesburg"]},{"windowsName":"FLE Standard Time","iana":["Europe/Kiev"]},{"windowsName":"Israel Standard Time","iana":["Asia/Jerusalem"]},{"windowsName":"Kaliningrad Standard Time","iana":["Europe/Kaliningrad"]},{"windowsName":"Sudan Standard Time","iana":["Africa/Khartoum"]},{"windowsName":"Libya Standard Time","iana":["Africa/Tripoli"]},{"windowsName":"Namibia Standard Time","iana":["Africa/Windhoek"]},{"windowsName":"Arabic Standard Time","iana":["Asia/Baghdad"]},{"windowsName":"Turkey Standard Time","iana":["Europe/Istanbul"]},{"windowsName":"Arab Standard Time","iana":["Asia/Riyadh"]},{"windowsName":"Belarus Standard Time","iana":["Europe/Minsk"]},{"windowsName":"Russian Standard Time","iana":["Europe/Moscow"]},{"windowsName":"E. Africa Standard Time","iana":["Africa/Nairobi"]},{"windowsName":"Iran Standard Time","iana":["Asia/Tehran"]},{"windowsName":"Arabian Standard Time","iana":["Asia/Dubai"]},{"windowsName":"Astrakhan Standard Time","iana":["Europe/Astrakhan"]},{"windowsName":"Azerbaijan Standard Time","iana":["Asia/Baku"]},{"windowsName":"Russia Time Zone 3","iana":["Europe/Samara"]},{"windowsName":"Mauritius Standard Time","iana":["Indian/Mauritius"]},{"windowsName":"Saratov Standard Time","iana":["Europe/Saratov"]},{"windowsName":"Georgian Standard Time","iana":["Asia/Tbilisi"]},{"windowsName":"Caucasus Standard Time","iana":["Asia/Yerevan"]},{"windowsName":"Afghanistan Standard Time","iana":["Asia/Kabul"]},{"windowsName":"West Asia Standard Time","iana":["Asia/Tashkent"]},{"windowsName":"Ekaterinburg Standard Time","iana":["Asia/Yekaterinburg"]},{"windowsName":"Pakistan Standard Time","iana":["Asia/Karachi"]},{"windowsName":"India Standard Time","iana":["Asia/Calcutta"]},{"windowsName":"Sri Lanka Standard Time","iana":["Asia/Colombo"]},{"windowsName":"Nepal Standard Time","iana":["Asia/Katmandu"]},{"windowsName":"Central Asia Standard Time","iana":["Asia/Almaty"]},{"windowsName":"Bangladesh Standard Time","iana":["Asia/Dhaka"]},{"windowsName":"Omsk Standard Time","iana":["Asia/Omsk"]},{"windowsName":"Myanmar Standard Time","iana":["Asia/Rangoon"]},{"windowsName":"SE Asia Standard Time","iana":["Asia/Bangkok"]},{"windowsName":"Altai Standard Time","iana":["Asia/Barnaul"]},{"windowsName":"W. Mongolia Standard Time","iana":["Asia/Hovd"]},{"windowsName":"North Asia Standard Time","iana":["Asia/Krasnoyarsk"]},{"windowsName":"N. Central Asia Standard Time","iana":["Asia/Novosibirsk"]},{"windowsName":"Tomsk Standard Time","iana":["Asia/Tomsk"]},{"windowsName":"China Standard Time","iana":["Asia/Shanghai"]},{"windowsName":"North Asia East Standard Time","iana":["Asia/Irkutsk"]},{"windowsName":"Singapore Standard Time","iana":["Asia/Singapore"]},{"windowsName":"W. Australia Standard Time","iana":["Australia/Perth"]},{"windowsName":"Taipei Standard Time","iana":["Asia/Taipei"]},{"windowsName":"Ulaanbaatar Standard Time","iana":["Asia/Ulaanbaatar"]},{"windowsName":"Aus Central W. Standard Time","iana":["Australia/Eucla"]},{"windowsName":"Transbaikal Standard Time","iana":["Asia/Chita"]},{"windowsName":"Tokyo Standard Time","iana":["Asia/Tokyo"]},{"windowsName":"North Korea Standard Time","iana":["Asia/Pyongyang"]},{"windowsName":"Korea Standard Time","iana":["Asia/Seoul"]},{"windowsName":"Yakutsk Standard Time","iana":["Asia/Yakutsk"]},{"windowsName":"Cen. Australia Standard Time","iana":["Australia/Adelaide"]},{"windowsName":"AUS Central Standard Time","iana":["Australia/Darwin"]},{"windowsName":"E. Australia Standard Time","iana":["Australia/Brisbane"]},{"windowsName":"AUS Eastern Standard Time","iana":["Australia/Sydney"]},{"windowsName":"West Pacific Standard Time","iana":["Pacific/Port_Moresby"]},{"windowsName":"Tasmania Standard Time","iana":["Australia/Hobart"]},{"windowsName":"Vladivostok Standard Time","iana":["Asia/Vladivostok"]},{"windowsName":"Lord Howe Standard Time","iana":["Australia/Lord_Howe"]},{"windowsName":"Bougainville Standard Time","iana":["Pacific/Bougainville"]},{"windowsName":"Russia Time Zone 10","iana":["Asia/Srednekolymsk"]},{"windowsName":"Magadan Standard Time","iana":["Asia/Magadan"]},{"windowsName":"Norfolk Standard Time","iana":["Pacific/Norfolk"]},{"windowsName":"Sakhalin Standard Time","iana":["Asia/Sakhalin"]},{"windowsName":"Central Pacific Standard Time","iana":["Pacific/Guadalcanal"]},{"windowsName":"Russia Time Zone 11","iana":["Asia/Kamchatka"]},{"windowsName":"New Zealand Standard Time","iana":["Pacific/Auckland"]},{"windowsName":"UTC+12","iana":["Etc/GMT-12"]},{"windowsName":"Fiji Standard Time","iana":["Pacific/Fiji"]},{"windowsName":"Chatham Islands Standard Time","iana":["Pacific/Chatham"]},{"windowsName":"UTC+13","iana":["Etc/GMT-13"]},{"windowsName":"Tonga Standard Time","iana":["Pacific/Tongatapu"]},{"windowsName":"Samoa Standard Time","iana":["Pacific/Apia"]},{"windowsName":"Line Islands Standard Time","iana":["Pacific/Kiritimati"]}];

let windowsName = "India Standard Time";
console.log("Windows name: ", windowsName );

// We must find the correct mapping from Windows to IANA timezone
let timezoneName = timezones.find(tz => tz.windowsName === windowsName);
let localTime = moment.tz("2019-06-04 00:00", timezoneName.iana[0]);

console.log("Local Time: ", localTime.format("YYYY-MM-DD HH:mm"));
console.log("UTC Time: ", localTime.utc().format("YYYY-MM-DD HH:mm"));

// And using IANA name (we can do this directly)
let ianaName = "Asia/Kolkata";
console.log("Iana:", ianaName );
localTime = moment.tz("2019-06-04 00:00", ianaName );

console.log("Local Time: ", localTime.format("YYYY-MM-DD HH:mm"));
console.log("UTC Time: ", localTime.utc().format("YYYY-MM-DD HH:mm"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.25/moment-timezone-with-data-10-year-range.js"></script>

答案 1 :(得分:0)

  

Microsoft提供了类似以下时区的时区详细信息:“印度标准时间”,我想找到以秒为单位的时区偏移量,以便对日期对象执行某些操作。

Date.prototype.getTimezoneOffset()获取本地用户的时区与UTC的偏移量(以分钟为单位)。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

var now = new Date()
console.log(now.getTimeZoneOffset())