'how to calculate timezone offset from given timezone [duplicate]
I have a case where I am trying to calculate timezoneOffset from selected timezone.
The sample I am saving the time in the database as UTC format, Now I am calculating the timezone as per user's location/timezone and setting the timezone offset.
How I can create mapping that -330 offset if from which timezone, I want to calculate it for all the timezone. Example [{ timezone: 'Australia/Sydney', offset: '660' }] and so on..
such that I can search for -330 offset is for which timeozne.
Suppose -330 offset if for which timezone? Where I can create mapping with offset and timezone.
Example User a is from Australia/Sydney then how can I check timezone offset from UTC?
Just a note that I have multiple timezones to support overall more than 30 time zones.
And I want something very accurate calculation.
I know momentjs using it I can do, but suddenly don't know-how.
with momentjs I get UTCoffser of Australia/Sydney //660
Can anyone help me derive the accurate date here?
Solution 1:[1]
If you have the IANA name for your timezone, you can pass it into Intl.DateTimeFormat and filter out and calculate the offset:
const getUTCOffset = timezone => Intl.DateTimeFormat([], {timeZone: timezone, timeZoneName: 'shortOffset'}).formatToParts().find(o => o.type === 'timeZoneName').value.match(/\-?\d+/)[0]*60;
console.log(getUTCOffset('Australia/Sydney'))
If you're running this code in the user's browser and want to use the user's timezone, then it's even simpler:
new Date().getTimezoneOffset()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | skara9 |
