简体   繁体   中英

how to calculate timezone offset from given timezone

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM