Can we convert time zone city (America/New_York) to time zone region (Eastern)?

Viewed 43

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.

1 Answers

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

Related