1

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?

  • Does this answer your question? [How to convert UTC hours to local using Javascript](https://stackoverflow.com/questions/70647828/how-to-convert-utc-hours-to-local-using-javascript) – Mayur Vaghasiya Jan 27 '22 at 04:41
  • no @MayurVaghasiya – Monty Pererra Jan 27 '22 at 04:48
  • 1
    Impossible. Multiple timezones may use the same offset simultaneously, you can't reliably determine the timezone just from the offset. BTW, the subject is getting offset from timezone, which is fine. But the actual question is getting the timezone from the offset, which is not. Australia/Sydney is sometimes +10 and other times (such as currently) +11. There are many, many other places and timezones that use +10. – RobG Jan 27 '22 at 13:26

1 Answers1

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()
skara9
  • 4,042
  • 1
  • 6
  • 21
  • 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. – Monty Pererra Jan 27 '22 at 04:50
  • @MontyPererra you want to get a timezone name from a number? – skara9 Jan 27 '22 at 04:53
  • 1
    multiple timezones can have the same utc offset, so I don't see how that will work - maybe elaborate on the exact problem you are trying to solve so you can find a better solution – skara9 Jan 27 '22 at 04:55
  • I want to take input from the user that which timezone is from and based on that I have to convert UTC with offset and store it in a database such that it would I can perform actions. (But it needs to be accurate) Not sure how accurate this solution would be, can you suggest any? – Monty Pererra Jan 30 '22 at 05:45