0

Is there anyway to get utc offset from a timezone abbreviations? Such as:

"PST"

for

Pacific Standard Time (North America)

which will result in UTC−08. I found a way on moment-timezone but it did not help much. It provides a list of timezone identifier but most of them are deprecated. Thank you.

Nguyen Hoang Vu
  • 751
  • 2
  • 14
  • 35
  • 1
    Can you elaborate more on what you found using moment-timezone and how that wasn’t helpful? – deceze Feb 10 '20 at 09:09
  • As I said, it provides a list of identifier but most of them are deprecated. – Nguyen Hoang Vu Feb 10 '20 at 09:13
  • I think it does not help much when I show the list to end user for choosing their timezone – Nguyen Hoang Vu Feb 10 '20 at 09:16
  • If the goal is to let the user select their timezone, you shouldn't use the timezone abbreviations in the first place. You should use timezones like "Europe/London". 'Cause such timezones switch between "abbreviation timezones" at different times of the year. – deceze Feb 10 '20 at 09:19
  • does moment change it automatically? Between normal and DST – Nguyen Hoang Vu Feb 10 '20 at 09:26
  • If you use a timezone like "Europe/London" and you create a timestamp with that or you do some date calculations (e.g. adding "6 months" to a timestamp) or you convert a timestamp from one timezone to another, then yes, DST will be handled properly. That's the point of those kinds of timezones. – deceze Feb 10 '20 at 09:33
  • Thank you very much. I find a way through – Nguyen Hoang Vu Feb 10 '20 at 09:57

1 Answers1

4

This is a quite tricky topic and similar questions have already been discussed.

See Detect timezone abbreviation using JavaScript or Get timezone abbreviation using offset value

I would propose two options:

1. Hash table. Create a key-value pair of what you require based on standard information (e.g. list of timezone abbreviations and their offset). Something like

    let offsetByAbbrev = {"PST" : "UTC-08", "EST" : "UTC+01", ... }

But this needs serious maintenance as it will get deprecated quickly.

2. Date manipulation. You can create a date in a specific timezone with momentjs, however, not with abbreviations. And there is a good reason for it: these abbreviations can change and can also cause ambiguity. You can check out the JSON used by momentjs-timezone: https://github.com/moment/moment-timezone/blob/develop/data/packed/latest.json which will give you a general sense of how chaotic it can be. For example do a simple ctrl+f search for PST and see the results.

So I would suggest using the timezone names - which you can actually create and use with momentjs-timezone. See How to create time in a specific time zone with moment.js where you can create a moment object based on the timezone name and that object will contain (as per the spec of moment timezone: https://momentjs.com/timezone/docs/#/zone-object/) the offset itself.

I know this does not exactly resolve your problem, though what you are trying to do is not necessarily the best approach to reach your an ideal solution.

Andrew Adam
  • 1,572
  • 9
  • 22