Get UTC offset from timezone abbreviations

后端 未结 1 1881
深忆病人
深忆病人 2020-12-22 06:31

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

\"PST\"

for

Pacific Standard

相关标签:
1条回答
  • 2020-12-22 06:53

    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.

    0 讨论(0)
提交回复
热议问题