In Node, how can I convert a time zone (e.g. Europe/Stockholm
) to a UTC offset (e.g. +1
or +2
), given a specific point in time? Temporal
seems to be solving this in the future, but until then? Is this possible natively, or do I need something like tzdb
?
I would recommend not to use tzdb
directly, but rather a date library that deals well with timezones. In particular, I found Luxon to be good for this, see their documentation about timezone handling. To get the offset, you just create a DateTime
with the desired timezone, then get its .offset
:
const dateInZone = DateTime.fromISO("2022-10-23T21:10:56Z", {
zone: "Europe/Stockholm"
});
console.log(dateInZone.offset)
Alternatively, create a Zone
instance and get its .offset()
at a particular timestamp:
const zone = new IANAZone("Europe/Stockholm");
console.log(zone.offset(Date.parse("2022-10-23T21:10:56Z")));