Get client time zone from browser

Is there a reliable way to get the time zone from the client’s browser? I saw the following links, but I want a more reliable solution.

Automatic time zone detection using JavaScript

JavaScript timezone definition

+112
javascript timezone browser
Aug 04 2018-11-11T00:
source share
8 answers

Look at this pageloom repository useful

download jstz.min.js and add the function to the html page

<script language="javascript"> function getTimezoneName() { timezone = jstz.determine() return timezone.name(); } </script> 

and call this function from your display tag

+78
Jan 24 '12 at 10:00
source share

After half a decade, we have a built-in way to do this! For modern browsers, I would use:

 const tz = Intl.DateTimeFormat().resolvedOptions().timeZone; console.log(tz); 

This returns an IANA time zone string, but not an offset . Find out more at MDN .

Compatibility table - as of March 2019, it works for 90% of browsers used around the world. Doesn't work in Internet Explorer .

+108
May 29 '16 at 16:38
source share

Often, when people look for "time zones", simply "UTC offset" is enough. for example, their server is at UTC + 5, and they want to know that their client is running at UTC-8.




In plain old javascript (new Date()).getTimezoneOffset()/60 will return the current number of hours of offset from UTC.

It is worth noting the possible "error" in the sign of the return value getTimezoneOffset() (from MDN documents) :

The time zone offset is the difference in minutes between UTC and local time. Please note that this means that the offset is positive if the local time zone is behind UTC, and negative if it is ahead. For example, for the time zone UTC + 10: 00 (Australia Standard Eastern Time, Vladivostok Time, Chamorro Standard Time), -600 will be returned.




However, I recommend that you use day.js for Javascript code related to time / date. In this case, you can get the UTC offset in the ISO 8601 format by doing:

 > dayjs().format("Z") "-08:00" 

It is probably worth mentioning that the client can easily fake this information.

(Note: this answer originally recommended https://momentjs.com/ , but Dayjs is a more modern, smaller alternative.)

+64
Jan 23 '15 at 19:15
source share

At the moment, the best option is probably jstz, as suggested by mbayloon's answer .

For completeness, it should be mentioned that it has a standard: Intl . You already see this in Chrome:

 > Intl.DateTimeFormat().resolved.timeZone "America/Los_Angeles" 

(This is actually not standard compliant, which is another reason to use the library)

+41
Oct 08 '13 at 15:54
source share

Here is jsfiddle

It contains the abbreviation of the user's current time zone.

Here is a sample code

 var tz = jstz.determine(); console.log(tz.name()); console.log(moment.tz.zone(tz.name()).abbr(new Date().getTime())); 
+5
Jul 02 '14 at 23:57
source share

I used an approach similar to the one made by Josh Fraser , which determines the time offset of the browser from UTC and whether it recognizes DST or not (but is somewhat simplified from its code):

 var ClientTZ = { UTCoffset: 0, // Browser time offset from UTC in minutes UTCoffsetT: '+0000S', // Browser time offset from UTC in '±hhmmD' form hasDST: false, // Browser time observes DST // Determine browser timezone and DST getBrowserTZ: function () { var self = ClientTZ; // Determine UTC time offset var now = new Date(); var date1 = new Date(now.getFullYear(), 1-1, 1, 0, 0, 0, 0); // Jan var diff1 = -date1.getTimezoneOffset(); self.UTCoffset = diff1; // Determine DST use var date2 = new Date(now.getFullYear(), 6-1, 1, 0, 0, 0, 0); // Jun var diff2 = -date2.getTimezoneOffset(); if (diff1 != diff2) { self.hasDST = true; if (diff1 - diff2 >= 0) self.UTCoffset = diff2; // East of GMT } // Convert UTC offset to ±hhmmD form diff2 = (diff1 < 0 ? -diff1 : diff1) / 60; var hr = Math.floor(diff2); var min = diff2 - hr; diff2 = hr * 100 + min * 60; self.UTCoffsetT = (diff1 < 0 ? '-' : '+') + (hr < 10 ? '0' : '') + diff2.toString() + (self.hasDST ? 'D' : 'S'); return self.UTCoffset; } }; // Onload ClientTZ.getBrowserTZ(); 

When loading, the ClientTZ.getBrowserTZ() function is ClientTZ.getBrowserTZ() , which sets:

  • ClientTZ.UTCoffset to the offset of the browser time from UTC in minutes (for example, CST is -360 minutes, which is -6.0 hours from UTC);
  • ClientTZ.UTCoffsetT to the offset in the form of '±hhmmD' (for example, '-0600D' ), where the suffix is D for DST and S for standard (non-DST);
  • ClientTZ.hasDST (true or false).

ClientTZ.UTCoffset provided in minutes instead of hours, because some time intervals have fractional hourly shifts (for example, +0415).

The purpose of ClientTZ.UTCoffsetT is to use it as a key in the time zone table (not specified here), for example, for the <select> drop-down list.

+2
Apr 28 '16 at 18:59
source share

you can use moment-time to guess the time zone:

 > moment.tz.guess() "America/Asuncion" 
+2
Sep 13 '16 at 6:18
source share

No. There is no single reliable way, and it never will be. Did you really think you could trust a customer?

-10
Nov 12
source share



All Articles