1

Title says it all. Is there a way to convert a time zone by city to a time zone by region?

Ideally without a hardcoded map, but with built in Date functions?

examples:

America/New_York => Eastern
America/Chicago => Eastern
America/Los_Angeles  => Pacific

I can already get the offset and local time from the time zones I have now. I just want to see if there's a way to get the "parent" time zone for ui/orgnaization purposes.

Chang Yea Moon
  • 189
  • 1
  • 1
  • 10
  • Does this answer your question? [how to get time zone of city or country in javascript](https://stackoverflow.com/questions/37001869/how-to-get-time-zone-of-city-or-country-in-javascript) – ControlAltDel Sep 06 '22 at 18:31
  • This possible duplicate was from 6 years ago, and things may have changed. If I were you I'd search through the ecma script version changes and see if anything's changed – ControlAltDel Sep 06 '22 at 18:32
  • @ControlAltDel I'm not sure it does. I have the specific timezone, but I want to know if there is a way to get its "parent" timezone. Examples being listed in the question. It's primarily for ui purposes. I can already get the right local time and offset with the timezones i have right now – Chang Yea Moon Sep 06 '22 at 21:12

1 Answers1

2

I'd suggest looking at DateTimeFormat, you can use this to format the timezone in various ways, using the timeZoneName option.

We can wrap this up in a getAlernativeName() function.

function getAlternativeName(timeZone) {
    const opts = { timeZoneName: 'longGeneric', timeZone};
    return Intl.DateTimeFormat('en-EN', opts).format(Date.now()).split(',')[1];
}

let inputs = ['America/New_York', 'America/Louisville', 'America/Chicago', 'America/Denver' , 'America/Los_Angeles', 'Europe/Berlin']

console.log('Timezone'.padEnd(20, ' ') + ' Alternative Name')
inputs.forEach(input => { 
    console.log(input.padEnd(20, ' ') + getAlternativeName(input))
})
.as-console-wrapper { max-height: 100% !important; }
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40