2

Here I have an ISO string - 2023-02-23T14:30:00+11:00 I tried using luxon to get the timezone name but it returns "UTC+11" instead I wanted it to return a timezone name like Australia/Sydney. I do understand there will be different time zones with the same offset. But that is not a problem for me.

Is there a way to achieve this?

Here is my code snippet

let dt = DateTime.fromISO('2023-02-23T14:30:00+11:00', { setZone: true });
console.log(dt.zone.name) // returns UTC+11
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Udhaya
  • 291
  • 1
  • 3
  • 10
  • Does `dt.zoneName` give you the same thing? – Pointy Feb 20 '23 at 17:55
  • yes @Pointy it results the same – Udhaya Feb 20 '23 at 18:03
  • there are several areas on earth using such a value. I don't see how a function could guess which one you prefer to find. Otherwise you can consult their [list on wikipedia](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones), and I imagine that there is a JSON or XML file with all this information – Mister Jojo Feb 20 '23 at 18:28
  • +11:00 isn't a timezone, it's an offset. There are 11 locations in the IANA timezone database that use +11 for standard time, and two more that use it for daylight saving time. These relate to at least 2 regional timezones for standard time and DST. +11 is also designated by the military "L" (Lima) timezone. – RobG Feb 22 '23 at 04:01

2 Answers2

3

This shouldn't be possible. The issue is that you have a fixed-offset time zone, not an IANA time zone (see this article). The issue is set out in the documentation:

Multiple zones can have the same offset (think about the US's zones and their Canadian equivalents), though they might not have the same offset all the time, depending on when their DSTs are. Thus zones and offsets have a many-to-many relationship.

However, there is some hackery you can do. The tz NPM package has compiled all the timezones, so you can do something like this:

const {DateTime, Zone} = require('luxon');
const tz = require('tz');

function getTimeZoneName(dt) {
  let n = dt.zone.name.replace('UTC', '').replace(':', '');
  const sign = n[0];
  n = n.substring(1);
  if (n.length === 2) n = n + '00' 
  n = n.padStart(4, '0');
  return tz.tz_list[sign === '+' ? `+${n}` : `-${n}`];
}

let dt = DateTime.fromISO('2023-02-23T14:30:00+11:00', { setZone: true });
console.log(getTimeZoneName(dt)) // prints list of all possible timezones.

From there you might be able to get a city name, but its probably better to not use IANA time zones.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
2

+11:00 is an offset that correlates to a number of timezones. It is impossible to determine which timezone based only on the offset, or offset with date and time. It's possible to rule out a couple of timezones that use +11 for DST depending on the date and time.

The following lists the civil timezone names for the IANA representative locations that observe GMT+11 as either standard or daylight saving time.

Etc/GMT-11 (which is equivalent to GMT+11) is included for completeness. It doesn't represent either a timezone or location, it's just an offset.

let d = new Date(2023,0,1);
let tz = [
'Antarctica/Casey',
'Australia/Lord_Howe',
'Asia/Magadan',
'Asia/Sakhalin',
'Asia/Srednekolymsk',
'Etc/GMT-11',
'Pacific/Bougainville',
'Pacific/Efate',
'Pacific/Guadalcanal',
'Pacific/Kosrae',
'Pacific/Norfolk',
'Pacific/Noumea',
'Pacific/Pohnpei',
'Pacific/Ponape',
'Antarctica/Macquarie'].forEach(
  tz => console.log(tz+' - '+d.toLocaleString('en-GB', {timeZone: tz, timeZoneName:'long'}).substring(20))
);
RobG
  • 142,382
  • 31
  • 172
  • 209