Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
79
rated 0 times [  82] [ 3]  / answers: 1 / hits: 16284  / 8 Years ago, tue, november 8, 2016, 12:00:00

Using Javascript, is there a way to get a user's timezone name (PDT, EST, etc.) based on the user's device?



Code I tried:



const timezone = jstz.determine()
const userTimezone = timezone.name()


But I would like to get back the user's timezone name in PDT, EST, etc. instead of America/New_York.


More From » datetime

 Answers
4

Using moment.js with the moment-timezone add-on, you can do the following. The results will be consistent regardless of browser or operating system, because the abbreviations are in the data provided by the library.



The example results are from a computer set to the US Pacific time zone:



var zone = moment.tz.guess();            // America/Los_Angeles
var abbr = moment.tz(zone).format(z); // either PST or PDT, depending on when run


DISCLAIMER



Time zone abbreviations are not reliable or consistent. There are many different lists of them, and there are many ambiguities, such as CST being for Central Standard Time, Cuba Standard Time and China Standard Time. In general, you should avoid using them, and you should never parse them.



Also, the abbreviations in moment-timezone come from the IANA TZ Database. In recent editions of this data, many invented abbreviations have been replaced with numerical offsets instead. For example, if your user's time zone is Asia/Vladivostok, instead of getting VLAT like you may have in previous versions, you will instead get +10. This is because the abbreviation VLAT was originally invented by the tz database to fill the data, and is not in actual use by persons in the area. Keep in mind that abbreviations are always in English also - even in areas where other language abbreviations might be in actual use.


[#60134] Sunday, November 6, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
micayla

Total Points: 148
Total Questions: 92
Total Answers: 109

Location: Aruba
Member since Sat, Oct 2, 2021
3 Years ago
micayla questions
Fri, Dec 24, 21, 00:00, 2 Years ago
Thu, Apr 16, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
;