227

I know how to get the timezone offset, but what I need is the ability to detect something like "America/New York." Is that even possible from JavaScript or is that something I am going to have to guestimate based on the offset?

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • 2
    Here is a function Jon Nylander wrote, maybe it helps https://bitbucket.org/pellepim/jstimezonedetect – yunzen Mar 19 '12 at 15:28
  • Here's a related answer that might help: http://stackoverflow.com/a/19421672/1447034 – Douglas Apr 13 '15 at 10:06
  • Possible duplicate of [Detect timezone abbreviation using JavaScript](http://stackoverflow.com/questions/1954397/detect-timezone-abbreviation-using-javascript) – jberryman Oct 24 '15 at 20:33

10 Answers10

475

The Internationalization API supports getting the user timezone, and is supported in all current browsers.

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

Keep in mind that on some older browser versions that support the Internationalization API, the timeZone property is set to undefined rather than the user’s timezone string. As best as I can tell, at the time of writing (July 2017) all current browsers except for IE11 will return the user timezone as a string.

Daniel Compton
  • 13,878
  • 4
  • 40
  • 60
  • 2
    Returns `undefined` in Firefox: `let timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;` – ViaTech Aug 12 '19 at 14:51
  • 2
    `let` anything will return `undefined`, you need to evaluate `timezone`, or only run `Intl.DateTimeFormat().resolvedOptions().timeZone` – Daniel Compton Aug 13 '19 at 21:06
57

Most upvoted answer is probably the best way to get the timezone, however, Intl.DateTimeFormat().resolvedOptions().timeZone returns IANA timezone name by definition, which is in English.

If you want the timezone's name in current user's language, you can parse it from Date's string representation like so:

function getTimezoneName() {
  const today = new Date();
  const short = today.toLocaleDateString(undefined);
  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

  // Trying to remove date from the string in a locale-agnostic way
  const shortIndex = full.indexOf(short);
  if (shortIndex >= 0) {
    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    
    // by this time `trimmed` should be the timezone's name with some punctuation -
    // trim it from both sides
    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

  } else {
    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
    return full;
  }
}

console.log(getTimezoneName());

Tested in Chrome and Firefox.

Ofcourse, this will not work as intended in some of the environments. For example, node.js returns a GMT offset (e.g. GMT+07:00) instead of a name. But I think it's still readable as a fallback.

P.S. Won't work in IE11, just as the Intl... solution.

mrnateriver
  • 1,935
  • 1
  • 19
  • 19
  • This is a great solution. I am using the `timeZoneName: 'short'` to get the short form like EST, or GMT+0230 which I can use on the Java backend. – John Yeary May 25 '21 at 14:05
23

A short possibility for a result in current user's language:

console.log(new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long' }).substring(4));
maeurer
  • 231
  • 2
  • 2
  • 2
    I find this to be the most user-friendly example, since the user may not be located in the city returned by the other examples. For people looking for just the abbreviation like `PDT` or `EST` you can use this variation: new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'short' }).substring(4) – ninapavlich Mar 15 '22 at 23:02
8

If you're already using Moment.js you can guess the timezone name:

moment.tz.guess(); // eg. "America/New York"
Joel Mellon
  • 3,672
  • 1
  • 26
  • 25
Omer
  • 832
  • 1
  • 9
  • 10
  • 50
    Please, mention that it is also simply possible without any dependencies: `Intl.DateTimeFormat().resolvedOptions().timeZone` – user2923322 Jan 25 '20 at 06:13
  • 4
    Yep, although moment-js also guesses TZ for "browsers" that don't support that feature. :) – Ferran Maylinch May 25 '20 at 08:41
  • 7
    in IE11 `Intl.DateTimeFormat().resolvedOptions().timeZone` gives back `undefined`, but `moment.tz.guess()` returns correct value – Antoni4 Jun 04 '20 at 12:52
4

You can use this script. http://pellepim.bitbucket.org/jstz/

Fork or clone repository here. https://bitbucket.org/pellepim/jstimezonedetect

Once you include the script, you can get the list of timezones in - jstz.olson.timezones variable.

And following code is used to determine client browser's timezone.

var tz = jstz.determine();
tz.name();

Enjoy jstz!

sean
  • 1,644
  • 1
  • 15
  • 14
1

console.log(new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long' }).substring(4).match(/\b(\w)/g).join(''))
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0

You can simply write your own code by using the mapping table here: http://www.timeanddate.com/time/zones/

or, use moment-timezone library: http://momentjs.com/timezone/docs/

See zone.name; // America/Los_Angeles

or, this library: https://github.com/Canop/tzdetect.js

advncd
  • 3,787
  • 1
  • 25
  • 31
-3

To detect something like "America/New York.", you can use the new LocalZone() from the Luxon library.

import { LocalZone } from 'luxon';

const zoneName = new LocalZone().name;
Hugo Sohm
  • 2,872
  • 4
  • 23
  • 40
-4

This gets the timezone code (e.g., GMT) in older javascript (I'm using google app script with old engine):

function getTimezoneName() {
  return new Date().toString().get(/\((.+)\)/);
}

I'm just putting this here in case someone needs it.

toddmo
  • 20,682
  • 14
  • 97
  • 107
  • 1
    I think that only works for `UTC`/`GMT`. It looks like others are spelled out; e.g., `Pacific Daylight Time`. – Ian Dunn Apr 17 '20 at 17:28
-5

In javascript , the Date.getTimezoneOffset() method returns the time-zone offset from UTC, in minutes, for the current locale.

var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;

Moment'timezone will be a useful tool. http://momentjs.com/timezone/

Convert Dates Between Timezones

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00