20

I need to determine the client timezone (e.g. CET, GMT, EST) in JS. Getting the offset is straightforward, but doesn't have all the info necessary to determine the TZ, at least not easily. I have a feeling it might be possible to use a combination of offset, whether DST is in use and in effect, etc. but I'm hoping someone else has done the work already, especially considering the weird exceptions when dealing with time.

This question is similar but both the question and the answer are based on the (incorrect) assumption every browser's DateString format includes the name of the timezone - which it does not. That is an implementation detail.

So, with that I am currently stuck.

Community
  • 1
  • 1
Rex M
  • 142,167
  • 33
  • 283
  • 313
  • `Intl.DateTimeFormat().resolvedOptions().timeZone` is what we now have for this; see https://stackoverflow.com/a/44935836/441757 – sideshowbarker Apr 02 '21 at 01:20

5 Answers5

32

You could scrape it from a date object's toString() method. new Date.toString() results in something like Wed Sep 19 2012 10:04:32 GMT-0400 (Eastern Daylight Time) in Firefox, Safari and Chrome. The data you want is there -- the capital letters within the parentheses. All that's needed is to scrape them.

In the case of Internet Explorer, the result of toString() already includes the EDT.

In the case of Opera, your only option is to settle for GMT-0400 or similar. There's nothing scrape-able in the toString() method.

var now = new Date().toString();
var TZ = now.indexOf('(') > -1 ?
now.match(/\([^\)]+\)/)[0].match(/[A-Z]/g).join('') :
now.match(/[A-Z]{3,4}/)[0];
if (TZ == "GMT" && /(GMT\W*\d{4})/.test(now)) TZ = RegExp.$1;

Example results:

  • Firefox: TZ = "EDT"

  • Safari: TZ = "EDT"

  • Chrome: TZ = "EDT"

  • IE: TZ = "EDT"

  • Opera: TZ = "GMT-0400"

Seems to work just as well with all the random Asian and European time zones I tried as well, returning WPST for Guam (West Pacific Standard Time), MPST for Kuala Lumpur (Malay Peninsula Standard Time), etc; and degrades peacefully to GMT+0X00 notation where the browser doesn't supply the name (Perth, for example).

rojo
  • 24,000
  • 5
  • 55
  • 101
  • Fantastic. Exactly what ALOT of people are looking for I imagine – Yablargo Jun 13 '13 at 02:37
  • 1
    Thanks for this - we're using it. But it does not work in all parts of the world. It throws a an error in Saudi Arabia, for example. *Cannot call method 'join' of null*. For now I'm wrapping it in a try/catch, and setting TZ to 'my local time' as a quick fix. I'm sure there's a better fix to be done, but this one is quick :) – tom Mar 14 '14 at 18:50
  • p.s. I presume the issue is that the Date.toString() is not returning a value using the Roman Alphabet, so the regex is not matching – tom Mar 14 '14 at 19:06
  • Doesn't work for me, returns "MS" where Date.toString() = "Wed Jul 16 2014 13:13:29 GMT+0200 (Mitteleuropäische Sommerzeit)" (German Windows, Chrome) – Roland Jul 16 '14 at 11:14
7

See this question. It's not doable in the general case, but for picking a default timezone from a shortlist of likely cases, you can make a decent guess.

Allow the user to override it for when you guess wrong.

Community
  • 1
  • 1
bobince
  • 528,062
  • 107
  • 651
  • 834
  • Indeed, this is just needed to supply a decent guess. If it can prefill correctly for 90% of users I'd be very happy. – Rex M May 24 '10 at 17:45
  • Hi... with this, we ll get the time zone as say Aisa/kabul then how to convert it to 'AFT' (abbreviated form)?? – Jeevi May 02 '12 at 12:11
  • @TFool: unfortunately, there's no publically-available lookup for region->timezone-name that I know of, so you'll have to make your own. (Actually there isn't even a globally-accepted set of timezone abbreviations.) Bear in mind that there is not a one-to-one mapping: eg if you get the region you will have to decide whether to use the `GMT` or `BST` timezone depending on what part of the year the time you want to express is in. – bobince May 03 '12 at 16:29
  • Thanks.. So i need to have something like Hash-table(timezone - abbreviated values)... :) – Jeevi May 07 '12 at 12:14
1

What I would do is determining the standard and daylight offset (which sounds like you already knew. If not, you can start with this reference http://onlineaspect.com/examples/timezone/detect_timezone.js by Josh Fraser). Determining the time zone can always be done on the server side once the standard and daylight offsets are known. A Ajax call can then be used so that no page refresh is ever needed. End result is you now have the time zone on the JavaScript side.

Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80
0

Starting from this answer to another question, I found that IP API also gives you the timezone – pretty handy:

$.ajax({
    url: "http://ip-api.com/json",
    type: 'GET',
    success: function(json) {
        console.log("Timezone: " + json.timezone);
    },
    error: function(err) {
        console.log("Request failed, error= " + err);
    }
});
Community
  • 1
  • 1
Felix Böhme
  • 508
  • 5
  • 12
-4

If you just want offset, then this is much simpler

var curdate = new Date();

var offset = curdate.getTimezoneOffset();

vsingh
  • 6,365
  • 3
  • 53
  • 57