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
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.