如何使用时刻从时区标准名称中查找时间

How to find time from timezone standard name using moment

我有如下所示的时区信息 JSON。

{"timezone_standard_name": "(UTC-06:00) Central Time (US & Canada)"}.

是否可以找到上述时区的日期和时间。

我已经尝试找到 UTC 时间然后添加偏移量,但它只会给我 IST 中的日期和时间对象

偏移前

添加偏移后

这是我试过的。我们有更好的答案吗? 使用字符串操作 (UTC-06:00)

查找偏移量 +360
const tzNames = momenttz.tz.names();
const map = new Map();

for (const name of tzNames) {
  const offsets = momenttz.tz.zone(name).offsets;
  for (const offset of offsets) {
    if (!map.has(offset)) {
        map.set(offset, new Set());
    }
    map.get(offset).add(name);
  }
}
const offsetList = map.get(360);
for (const item of offsetList) {
  console.log(item);
  let tx = momenttz().tz(item).format();
  console.log('Time in given zone is = ' + tx);
}

您可以使用 Moment Timezone, we can call moment.tz(timezone) to get the time in that timezone. However moment requires an IANA timezone 获取另一个时区的时间,并且您有一个 Windows 时区名称。

我们可以使用包 windows-iana 进行转换,我创建了一个函数 findIANATimezone() 来为 Windows 时区名称找到相应的 IANA 时区。

在这种情况下,我们将为下面的示例找到等效的 IANA 时区,然后使用 moment.tz() 获取该时区的当前时间。

注意:我们不能简单地为时区添加 UTC 偏移量,因为由于夏令时,这会随着一年中的不同时间而变化。此外,一旦我们有了 IANA 时区,我们就可以使用任何库来获取该时区的当前时间:moment.js、luxon 等

let timezones = [
  { "timezone_standard_name": "(UTC-06:00) Central Time (US & Canada)"},
  { "timezone_standard_name": "(UTC+00:00) Dublin, Edinburgh, Lisbon, London"},
  { "timezone_standard_name": "(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi"}
];

const colWidth = 50;

console.log(`Windows Timezone`.padEnd(colWidth, ' '), `IANA Timezone`); 
for(let { timezone_standard_name: name } of timezones) {
    console.log(name.padEnd(colWidth, ' '), findIANATimezone(name))
}

console.log(`\n`);

console.log(`Windows Timezone`.padEnd(colWidth, ' '), `Current Time`); 
for(let { timezone_standard_name: name } of timezones) {
    console.log(name.padEnd(colWidth, ' '), getCurrentTime(name).format('YYYY-MM-DD HH:mm'))
}


function getCurrentTime(timezoneName) { 
    let ianaTimezone = findIANATimezone(timezoneName);
    if (!ianaTimezone) { 
        return null;
    }
    return moment.tz(ianaTimezone);
}

function findIANATimezone(name) {
    const timeZones = [
      {
        id: 'Dateline Standard Time',
        name: '(UTC-12:00) International Date Line West',
        iana: 'Etc/GMT+12'
      },
      {
        id: 'Samoa Standard Time',
        name: '(UTC-11:00) Midway Island, Samoa',
        iana: 'Pacific/Apia'
      },
      {
        id: 'Hawaiian Standard Time',
        name: '(UTC-10:00) Hawaii',
        iana: 'Pacific/Honolulu'
      },
      {
        id: 'Alaskan Standard Time',
        name: '(UTC-09:00) Alaska',
        iana: 'America/Anchorage'
      },
      {
        id: 'Pacific Standard Time',
        name: '(UTC-08:00) Pacific Time (US & Canada)',
        iana: 'America/Los_Angeles'
      },
      {
        id: 'Pacific Standard Time (Mexico)',
        name: '(UTC-08:00) Tijuana, Baja California',
        iana: 'America/Tijuana'
      },
      {
        id: 'US Mountain Standard Time',
        name: '(UTC-07:00) Arizona',
        iana: 'America/Phoenix'
      },
      {
        id: 'Mountain Standard Time (Mexico)',
        name: '(UTC-07:00) Chihuahua, La Paz, Mazatlan',
        iana: 'America/Chihuahua'
      },
      {
        id: 'Mountain Standard Time',
        name: '(UTC-07:00) Mountain Time (US & Canada)',
        iana: 'America/Denver'
      },
      {
        id: 'Central America Standard Time',
        name: '(UTC-06:00) Central America',
        iana: 'America/Guatemala'
      },
      {
        id: 'Central Standard Time',
        name: '(UTC-06:00) Central Time (US & Canada)',
        iana: 'America/Chicago'
      },
      {
        id: 'Central Standard Time (Mexico)',
        name: '(UTC-06:00) Guadalajara, Mexico City, Monterrey',
        iana: 'America/Mexico_City'
      },
      {
        id: 'Canada Central Standard Time',
        name: '(UTC-06:00) Saskatchewan',
        iana: 'America/Regina'
      },
      {
        id: 'SA Pacific Standard Time',
        name: '(UTC-05:00) Bogota, Lima, Quito, Rio Branco',
        iana: 'America/Bogota'
      },
      {
        id: 'Eastern Standard Time',
        name: '(UTC-05:00) Eastern Time (US & Canada)',
        iana: 'America/New_York'
      },
      {
        id: 'US Eastern Standard Time',
        name: '(UTC-05:00) Indiana (East)',
        iana: 'America/Indianapolis'
      },
      {
        id: 'Venezuela Standard Time',
        name: '(UTC-04:30) Caracas',
        iana: 'America/Caracas'
      },
      {
        id: 'Atlantic Standard Time',
        name: '(UTC-04:00) Atlantic Time (Canada)',
        iana: 'America/Halifax'
      },
      {
        id: 'SA Western Standard Time',
        name: '(UTC-04:00) La Paz',
        iana: 'America/La_Paz'
      },
      {
        id: 'Central Brazilian Standard Time',
        name: '(UTC-04:00) Manaus',
        iana: 'America/Cuiaba'
      },
      {
        id: 'Pacific SA Standard Time',
        name: '(UTC-04:00) Santiago',
        iana: 'America/Santiago'
      },
      {
        id: 'Newfoundland Standard Time',
        name: '(UTC-03:30) Newfoundland',
        iana: 'America/St_Johns'
      },
      {
        id: 'E. South America Standard Time',
        name: '(UTC-03:00) Brasilia',
        iana: 'America/Sao_Paulo'
      },
      {
        id: 'Argentina Standard Time',
        name: '(UTC-03:00) Buenos Aires',
        iana: 'America/Buenos_Aires'
      },
      {
        id: 'SA Eastern Standard Time',
        name: '(UTC-03:00) Georgetown',
        iana: 'America/Cayenne'
      },
      {
        id: 'Greenland Standard Time',
        name: '(UTC-03:00) Greenland',
        iana: 'America/Godthab'
      },
      {
        id: 'Montevideo Standard Time',
        name: '(UTC-03:00) Montevideo',
        iana: 'America/Montevideo'
      },
      {
        id: 'Mid-Atlantic Standard Time',
        name: '(UTC-02:00) Mid-Atlantic',
        iana: undefined
      },
      {
        id: 'Azores Standard Time',
        name: '(UTC-01:00) Azores',
        iana: 'Atlantic/Azores'
      },
      {
        id: 'Cape Verde Standard Time',
        name: '(UTC-01:00) Cape Verde Is.',
        iana: 'Atlantic/Cape_Verde'
      },
      {
        id: 'Morocco Standard Time',
        name: '(UTC+00:00) Casablanca',
        iana: 'Africa/Casablanca'
      },
      {
        id: 'GMT Standard Time',
        name: '(UTC+00:00) Dublin, Edinburgh, Lisbon, London',
        iana: 'Europe/London'
      },
      {
        id: 'Greenwich Standard Time',
        name: '(UTC+00:00) Monrovia, Reykjavik',
        iana: 'Atlantic/Reykjavik'
      },
      {
        id: 'UTC',
        name: '(UTC+00:00) Coordinated Universal Time',
        iana: 'UTC'
      },
      {
        id: 'W. Europe Standard Time',
        name: '(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna',
        iana: 'Europe/Berlin'
      },
      {
        id: 'Central Europe Standard Time',
        name: '(UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague',
        iana: 'Europe/Budapest'
      },
      {
        id: 'Romance Standard Time',
        name: '(UTC+01:00) Brussels, Copenhagen, Madrid, Paris',
        iana: 'Europe/Paris'
      },
      {
        id: 'Central European Standard Time',
        name: '(UTC+01:00) Sarajevo, Skopje, Warsaw, Zagreb',
        iana: 'Europe/Warsaw'
      },
      {
        id: 'W. Central Africa Standard Time',
        name: '(UTC+01:00) West Central Africa',
        iana: 'Africa/Lagos'
      },
      {
        id: 'Jordan Standard Time',
        name: '(UTC+02:00) Amman',
        iana: 'Asia/Amman'
      },
      {
        id: 'GTB Standard Time',
        name: '(UTC+02:00) Athens, Bucharest, Istanbul',
        iana: 'Europe/Bucharest'
      },
      {
        id: 'Middle East Standard Time',
        name: '(UTC+02:00) Beirut',
        iana: 'Asia/Beirut'
      },
      {
        id: 'Egypt Standard Time',
        name: '(UTC+02:00) Cairo',
        iana: 'Africa/Cairo'
      },
      {
        id: 'South Africa Standard Time',
        name: '(UTC+02:00) Harare, Pretoria',
        iana: 'Africa/Johannesburg'
      },
      {
        id: 'FLE Standard Time',
        name: '(UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius',
        iana: 'Europe/Kiev'
      },
      {
        id: 'Israel Standard Time',
        name: '(UTC+02:00) Jerusalem',
        iana: 'Asia/Jerusalem'
      },
      {
        id: 'E. Europe Standard Time',
        name: '(UTC+02:00) Minsk',
        iana: 'Europe/Chisinau'
      },
      {
        id: 'Namibia Standard Time',
        name: '(UTC+02:00) Windhoek',
        iana: 'Africa/Windhoek'
      },
      {
        id: 'Arabic Standard Time',
        name: '(UTC+03:00) Baghdad',
        iana: 'Asia/Baghdad'
      },
      {
        id: 'Arab Standard Time',
        name: '(UTC+03:00) Kuwait, Riyadh',
        iana: 'Asia/Riyadh'
      },
      {
        id: 'Russian Standard Time',
        name: '(UTC+03:00) Moscow, St. Petersburg, Volgograd',
        iana: 'Europe/Moscow'
      },
      {
        id: 'E. Africa Standard Time',
        name: '(UTC+03:00) Nairobi',
        iana: 'Africa/Nairobi'
      },
      {
        id: 'Georgian Standard Time',
        name: '(UTC+03:00) Tbilisi',
        iana: 'Asia/Tbilisi'
      },
      {
        id: 'Iran Standard Time',
        name: '(UTC+03:30) Tehran',
        iana: 'Asia/Tehran'
      },
      {
        id: 'Arabian Standard Time',
        name: '(UTC+04:00) Abu Dhabi, Muscat',
        iana: 'Asia/Dubai'
      },
      {
        id: 'Azerbaijan Standard Time',
        name: '(UTC+04:00) Baku',
        iana: 'Asia/Baku'
      },
      {
        id: 'Mauritius Standard Time',
        name: '(UTC+04:00) Port Louis',
        iana: 'Indian/Mauritius'
      },
      {
        id: 'Caucasus Standard Time',
        name: '(UTC+04:00) Yerevan',
        iana: 'Asia/Yerevan'
      },
      {
        id: 'Afghanistan Standard Time',
        name: '(UTC+04:30) Kabul',
        iana: 'Asia/Kabul'
      },
      {
        id: 'Ekaterinburg Standard Time',
        name: '(UTC+05:00) Ekaterinburg',
        iana: 'Asia/Yekaterinburg'
      },
      {
        id: 'Pakistan Standard Time',
        name: '(UTC+05:00) Islamabad, Karachi',
        iana: 'Asia/Karachi'
      },
      {
        id: 'West Asia Standard Time',
        name: '(UTC+05:00) Tashkent',
        iana: 'Asia/Tashkent'
      },
      {
        id: 'India Standard Time',
        name: '(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi',
        iana: 'Asia/Calcutta'
      },
      {
        id: 'Sri Lanka Standard Time',
        name: '(UTC+05:30) Sri Jayawardenepura',
        iana: 'Asia/Colombo'
      },
      {
        id: 'Nepal Standard Time',
        name: '(UTC+05:45) Kathmandu',
        iana: 'Asia/Katmandu'
      },
      {
        id: 'N. Central Asia Standard Time',
        name: '(UTC+06:00) Almaty, Novosibirsk',
        iana: 'Asia/Novosibirsk'
      },
      {
        id: 'Central Asia Standard Time',
        name: '(UTC+06:00) Astana, Dhaka',
        iana: 'Asia/Almaty'
      },
      {
        id: 'Myanmar Standard Time',
        name: '(UTC+06:30) Yangon (Rangoon)',
        iana: 'Asia/Rangoon'
      },
      {
        id: 'SE Asia Standard Time',
        name: '(UTC+07:00) Bangkok, Hanoi, Jakarta',
        iana: 'Asia/Bangkok'
      },
      {
        id: 'North Asia Standard Time',
        name: '(UTC+07:00) Krasnoyarsk',
        iana: 'Asia/Krasnoyarsk'
      },
      {
        id: 'China Standard Time',
        name: '(UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi',
        iana: 'Asia/Shanghai'
      },
      {
        id: 'North Asia East Standard Time',
        name: '(UTC+08:00) Irkutsk, Ulaan Bataar',
        iana: 'Asia/Irkutsk'
      },
      {
        id: 'Singapore Standard Time',
        name: '(UTC+08:00) Kuala Lumpur, Singapore',
        iana: 'Asia/Singapore'
      },
      {
        id: 'W. Australia Standard Time',
        name: '(UTC+08:00) Perth',
        iana: 'Australia/Perth'
      },
      {
        id: 'Taipei Standard Time',
        name: '(UTC+08:00) Taipei',
        iana: 'Asia/Taipei'
      },
      {
        id: 'Tokyo Standard Time',
        name: '(UTC+09:00) Osaka, Sapporo, Tokyo',
        iana: 'Asia/Tokyo'
      },
      {
        id: 'Korea Standard Time',
        name: '(UTC+09:00) Seoul',
        iana: 'Asia/Seoul'
      },
      {
        id: 'Yakutsk Standard Time',
        name: '(UTC+09:00) Yakutsk',
        iana: 'Asia/Yakutsk'
      },
      {
        id: 'Cen. Australia Standard Time',
        name: '(UTC+09:30) Adelaide',
        iana: 'Australia/Adelaide'
      },
      {
        id: 'AUS Central Standard Time',
        name: '(UTC+09:30) Darwin',
        iana: 'Australia/Darwin'
      },
      {
        id: 'E. Australia Standard Time',
        name: '(UTC+10:00) Brisbane',
        iana: 'Australia/Brisbane'
      },
      {
        id: 'AUS Eastern Standard Time',
        name: '(UTC+10:00) Canberra, Melbourne, Sydney',
        iana: 'Australia/Sydney'
      },
      {
        id: 'West Pacific Standard Time',
        name: '(UTC+10:00) Guam, Port Moresby',
        iana: 'Pacific/Port_Moresby'
      },
      {
        id: 'Tasmania Standard Time',
        name: '(UTC+10:00) Hobart',
        iana: 'Australia/Hobart'
      },
      {
        id: 'Vladivostok Standard Time',
        name: '(UTC+10:00) Vladivostok',
        iana: 'Asia/Vladivostok'
      },
      {
        id: 'Central Pacific Standard Time',
        name: '(UTC+11:00) Magadan, Solomon Is., New Caledonia',
        iana: 'Pacific/Guadalcanal'
      },
      {
        id: 'New Zealand Standard Time',
        name: '(UTC+12:00) Auckland, Wellington',
        iana: 'Pacific/Auckland'
      },
      {
        id: 'Fiji Standard Time',
        name: '(UTC+12:00) Fiji, Kamchatka, Marshall Is.',
        iana: 'Pacific/Fiji'
      },
      {
        id: 'Tonga Standard Time',
        name: "(UTC+13:00) Nuku'alofa",
        iana: 'Pacific/Tongatapu'
      }
    ]
    const tz = timeZones.find(tz => tz.name === name);
    return tz ? tz.iana: null;
}
.as-console-wrapper { max-height: 100% !important; }
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-1970-2030.min.js"></script>