Search code examples
javascriptmomentjsmoment-timezone

How to find time from timezone standard name using moment


I have the time zone info as shown in below JSON.

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

Is it possible to find the date and time for the above time zone.

I have already tried to find UTC time then add the offset but it will just give me the date and time object in IST

Before offset

enter image description here

After adding offset

enter image description here

This is what I have tried. Do we have any better answer? Find the offset that is +360 using string manipulation (UTC-06:00)

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);
}

Solution

  • You can get the time in another timezone using Moment Timezone, we can call moment.tz(timezone) to get the time in that timezone. However moment requires an IANA timezone and you have a Windows timezone name.

    We can convert using the package windows-iana, I've created a function findIANATimezone() that finds the corresponding IANA timezone for a Windows timezone name.

    In this case, we'll find the equivalent IANA timezone for the examples below, then use moment.tz() to get the current time in that timezone.

    Note: We can't simply add the UTC offset for a timezone, since this will vary by the time of year, due to Daylight Saving Time. Also, once we have an IANA timezone, we can use any library to get the current time in that zone: moment.js, luxon etc.

    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>