8

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.

  • afaik this is not possible. You can get the timezone offset however with `(new Date).getTimezoneOffset()`; – Christoph Nov 08 '16 at 19:43
  • @Christoph Are you certain that it is not possible? –  Nov 08 '16 at 19:59
  • You should be able to do it with just the Date object and RegEx. Something like Date().match(/['\\(']...['\\)']/) – DottedT Nov 08 '16 at 20:09
  • @DottedT Sorry but could you show for clarification? Is there a way to do it without RegEx though? Also so I can accept the answer as well. –  Nov 08 '16 at 20:16
  • @DottedT - That's not reliable. Anything after the date and time in parenthesis is implementation specific. In some cases you'll get an abbreviation, in others you'll get a whole time zone name, and the names vary considerably. There is no standard for this. – Matt Johnson-Pint Nov 08 '16 at 20:16
  • With vanilla javascript, the best shot you have is `date.toString()` which leaves you with something like `Day Time GMT+-xxx0 (Timezone)`. The format of this however depends on the client (browser) and the OS language, so it's merely a guess. `timezoneOffset` is the only reliable information you have. For example, my browser gave me something weird like "Eastern Normalzeit" as Timezone. – Christoph Nov 08 '16 at 20:18
  • @Christoph - Yes, and also the offset is not the same as the time zone. See "time zone != offset" in [the timezone tag wiki](http://stackoverflow.com/tags/timezone/info). – Matt Johnson-Pint Nov 08 '16 at 20:20
  • Mat, Good catch on the reliability. I forgot about that. – DottedT Nov 08 '16 at 20:23
  • @Christoph - yep, was a dup, but not of the one you linked. Didn't recall I had already answered it elsewhere before I wrote the below. Oh well, I'll leave it. :) Cheers! – Matt Johnson-Pint Nov 08 '16 at 20:26
  • Intl.DateTimeFormat().resolvedOptions().timeZone – bronze man Jul 18 '18 at 10:52

1 Answers1

10

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thank you so much! Will give it a try and accept the answer. A few questions I have if you don't mind. So what do you suggest if I want to display the timezone name? Would `America/Los_Angeles` be better? –  Nov 08 '16 at 21:04
  • The data you're looking for would be something like "Eastern Standard Time" spelled out in English. There's not a great answer for JavaScript yet, but this is in the [CLDR](http://cldr.unicode.org/). There's access via the Intl API that support it, with the `timeZone` and `timeZoneName` options on [`toLocaleString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString). But these don't work everywhere yet. – Matt Johnson-Pint Nov 08 '16 at 21:28
  • Of course, this depends greatly on context. Abbreviations may indeed be OK for your usage. Or not. You haven't said anything about your usage. – Matt Johnson-Pint Nov 08 '16 at 21:29
  • Gotcha! And one more question, what is `tz` representing? –  Nov 09 '16 at 02:24
  • That is part of the moment-timezone API. [The docs are here](http://momentjs.com/timezone/docs/). – Matt Johnson-Pint Nov 09 '16 at 20:00
  • Worked like a charm. Thank you Matt! –  Nov 10 '16 at 00:04