# Getting the client’s timezone offset in JavaScript

## The Question :

687 people think this question is useful

How can I gather the visitor’s time zone information? I need the Timezone, as well as the GMT offset hours.

• I’m figting the same problem now. Here is an interesting approach.
• The title asks for timezone, while the text asks for GMT offset. These are 2 different things. A Timezone includes information about DST, utc offset does not. A Timezone has a name and history, offset does not. Recommend to edit the title to “Getting the client’s gmt offset in JS”.
• I need the timezone. The GMT offset would be helpful too.
• The answers here focus mostly on offset. For time zone see this answer. See “Time Zone != Offset” in the timezone tag wiki.
• You get exact Timezone by using this Intl.DateTimeFormat().resolvedOptions().timeZone

638 people think this answer is useful

var offset = new Date().getTimezoneOffset();
console.log(offset);


The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight savings time prevents this value from being a constant even for a given locale

Note that not all timezones are offset by whole hours: for example, Newfoundland is UTC minus 3h 30m (leaving Daylight Saving Time out of the equation).

612 people think this answer is useful

Using an offset to calculate Timezone is a wrong approach, and you will always encounter problems. Time zones and daylight saving rules may change on several occasions during a year, and It’s difficult to keep up with changes.

To get the system’s IANA timezone in JavaScript, you should use

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


## As of September 2019, this works in 95% of the browsers used globally.

### Old compatibility information

ecma-402/1.0 says that timeZone may be undefined if not provided to constructor. However, future draft (3.0) fixed that issue by changing to system default timezone.

In this version of the ECMAScript Internationalization API, the timeZone property will remain undefined if no timeZone property was provided in the options object provided to the Intl.DateTimeFormat constructor. However, applications should not rely on this, as future versions may return a String value identifying the host environment’s current time zone instead.

in ecma-402/3.0 which is still in a draft it changed to

In this version of the ECMAScript 2015 Internationalization API, the timeZone property will be the name of the default time zone if no timeZone property was provided in the options object provided to the Intl.DateTimeFormat constructor. The previous version left the timeZone property undefined in this case.

130 people think this answer is useful

I realize this answer is a bit off topic but I imagine many of us looking for an answer also wanted to format the time zone for display and perhaps get the zone abbreviation too. So here it goes…

If you want the client timezone nicely formatted you can rely on the JavaScript Date.toString method and do:

var split = new Date().toString().split(" ");
var timeZoneFormatted = split[split.length - 2] + " " + split[split.length - 1];



This will give you “GMT-0400 (EST)” for example, including the timezone minutes when applicable.

Alternatively, with regex you can extract any desired part:

For “GMT-0400 (EDT)” :

new Date().toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1]



For “GMT-0400” :

new Date().toString().match(/([A-Z]+[\+-][0-9]+)/)[1]



For just “EDT” :

new Date().toString().match(/$$([A-Za-z\s].*)$$/)[1]



For just “-0400”:

new Date().toString().match(/([-\+][0-9]+)\s/)[1]



Date.toString reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/toString

EDIT 10/6/2020 – The above solution may not work in all browsers and locales. My present recommendation is to utilize a library. Some good popular libraries are: moment, date-fns 2, luxon or dayjs.

73 people think this answer is useful

It’s already been answered how to get offset in minutes as an integer, but in case anyone wants the local GMT offset as a string e.g. "+1130":

function pad(number, length){
var str = "" + number
while (str.length < length) {
str = '0'+str
}
return str
}

var offset = new Date().getTimezoneOffset()
offset = ((offset<0? '+':'-')+ // Note the reversed sign!



72 people think this answer is useful

You can use:

# moment-timezone

<script src="moment.js"></script>
<script src="moment-timezone-with-data.js"></script>

// retrieve timezone by name (i.e. "America/Chicago")
moment.tz.guess();



Browser time zone detection is rather tricky to get right, as there is little information provided by the browser.

Moment Timezone uses Date.getTimezoneOffset() and Date.toString() on a handful of moments around the current year to gather as much information about the browser environment as possible. It then compares that information with all the time zone data loaded and returns the closest match. In case of ties, the time zone with the city with largest population is returned.

console.log(moment.tz.guess()); // America/Chicago



49 people think this answer is useful

I wrote a function in my project, which returns the timezone in hh:mm format. I hope this may help someone:

function getTimeZone() {
var offset = new Date().getTimezoneOffset(), o = Math.abs(offset);
return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2);
}



// Outputs: +5:00



function getTimeZone() {
var offset = new Date().getTimezoneOffset(), o = Math.abs(offset);
return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2);
}

// See output
document.write(getTimeZone());


Working Fiddle

20 people think this answer is useful

A one-liner that gives both the offset and the time zone is to simply call toTimeString() on a new Date object. From MDN:

The toTimeString() method returns the time portion of a Date object in human readable form in American English.

The catch is that the timezone is not in the standard IANA format; it’s somewhat more user-friendly, than the “continent/city” IANA format. Try it out:

console.log(new Date().toTimeString().slice(9));
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);
console.log(new Date().getTimezoneOffset() / -60);


In California right now, toTimeString() returns Pacific Daylight Time while the Intl API returns America/Los_Angeles. In Colombia, you’d get Colombia Standard Time, vs. America/Bogota.

Note that many other answers to this question attempt to obtain the same information by calling Date.toString(). That approach is not that reliable, as MDN explains:

Date instances refer to a specific point in time. Calling toString() will return the date formatted in a human readable form in American English. […] Sometimes it is desirable to obtain a string of the time portion; such a thing can be accomplished with the toTimeString() method.

The toTimeString() method is especially useful because compliant engines implementing ECMA-262 may differ in the string obtained from toString() for Date objects, as the format is implementation-dependent; simple string slicing approaches may not produce consistent results across multiple engines.

13 people think this answer is useful

try getTimezoneOffset() of the Date object:

var curdate = new Date()
var offset = curdate.getTimezoneOffset()



This method returns time zone offset in minutes which is the difference between GMT and local time in minutes.

9 people think this answer is useful

JavaScript:

var d = new Date();
var n = d.getTimezoneOffset();
var timezone = n / -60;
console.log(timezone);


7 people think this answer is useful

With moment.js:

moment().format('zz');



6 people think this answer is useful

With momentjs, you can find current timezone as

console.log(moment().utcOffset()); // (-240, -120, -60, 0, 60, 120, 240, etc.)

<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>


With dayjs, you can find current timezone as

console.log(dayjs().utcOffset()); // (-240, -120, -60, 0, 60, 120, 240, etc.)

<script src="https://unpkg.com/dayjs@1.8.10/dayjs.min.js"></script>


Both API returns utc offset in minutes.

4 people think this answer is useful

Timezone in hours-

var offset = new Date().getTimezoneOffset();
if(offset<0)
console.log( "Your timezone is- GMT+" + (offset/-60));
else
console.log( "Your timezone is- GMT-" + offset/60);


If you want to be precise as you mentioned in comment, then you should try like this-

var offset = new Date().getTimezoneOffset();

if(offset<0)
{
var extraZero = "";
if(-offset%60<10)
extraZero="0";

console.log( "Your timezone is- GMT+" + Math.ceil(offset/-60)+":"+extraZero+(-offset%60));
}
else
{
var extraZero = "";
if(offset%60<10)
extraZero="0";

console.log( "Your timezone is- GMT-" + Math.floor(offset/60)+":"+extraZero+(offset%60));
}


3 people think this answer is useful

If all you need is the “MST” or the “EST” time zone abbreviation:

function getTimeZone(){
var now = new Date().toString();
var timeZone = now.replace(/.*[(](.*)[)].*/,'$1');//extracts the content between parenthesis return timeZone; }  ## The Answer 14 3 people think this answer is useful See this resultant operator was opposite to the Timezone .So apply some math function then validate the num less or more. var a = new Date().getTimezoneOffset(); var res = -Math.round(a/60)+':'+-(a%60); res = res < 0 ?res : '+'+res; console.log(res)  ## The Answer 15 3 people think this answer is useful function getLocalTimeZone() { var dd = new Date(); var ddStr = dd.toString(); var ddArr = ddStr.split(' '); var tmznSTr = ddArr[5]; tmznSTr = tmznSTr.substring(3, tmznSTr.length); return tmznSTr; }  Example : Thu Jun 21 2018 18:12:50 GMT+0530 (India Standard Time) O/P : +0530 ## The Answer 16 3 people think this answer is useful Try this, new Date().toString().split("GMT")[1].split(" (")[0]  ## The Answer 17 3 people think this answer is useful Try this : new Date().toLocaleString("en-US",Intl.DateTimeFormat().resolvedOptions().timeZone)  This will look for timeZone on your client’s browser. ## The Answer 18 2 people think this answer is useful This value is from user’s machine and it can be changed anytime so I think it doesn’t matter, I just want to get an approximate value and then convert it to GMT in my server. For example, I am from Taiwan and it returns “+8” for me. JS function timezone() { var offset = new Date().getTimezoneOffset(); var minutes = Math.abs(offset); var hours = Math.floor(minutes / 60); var prefix = offset < 0 ? "+" : "-"; return prefix+hours; }$('#result').html(timezone());



HTML

<div id="result"></div>



Result

+8



2 people think this answer is useful

On the new Date() you can get the offset, to get the timezone name you may do:

new Date().toString().replace(/(.*$$(.*)$$.*)/, '$2'); you get the value between () in the end of the date, that is the name of the timezone. ## The Answer 20 2 people think this answer is useful This would be my solution: <pre class="wp-block-syntaxhighlighter-code"> <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> // For time zone: const timeZone = /$$(.*)$$/.exec(new Date().toString())[1]; // Offset hours: const offsetHours = new Date().getTimezoneOffset() / 60; console.log(${timeZone}, ${offsetHours}hrs); </div> </div> </pre>  ## The Answer 21 1 people think this answer is useful As an alternative to new Date().getTimezoneOffset() and moment().format('zz'), you can also use momentjs: var offset = moment.parseZone(Date.now()).utcOffset() / 60 console.log(offset);  <script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>  jstimezone is also quite buggy and unmaintained (https://bitbucket.org/pellepim/jstimezonedetect/issues?status=new&status=open) ## The Answer 22 1 people think this answer is useful Use this to convert OffSet to postive: var offset = new Date().getTimezoneOffset(); console.log(offset); this.timeOffSet = offset + (-2*offset); console.log(this.timeOffSet);  ## The Answer 23 0 people think this answer is useful If you just want the timezone (like IST, GMT, etc.), then use this: var timezone = new Date().toString().match(/$$([A-Za-z\s].*)$$/)[1]; var parts = timezone.split(' '); var tz = ""; parts.forEach(function (element) { tz += element.substring(0, 1); });  ## The Answer 24 0 people think this answer is useful Once I had this “simple” task and I used (new Date()).getTimezoneOffset() – the approach that is widely suggested here. But it turned out that the solution wasn’t quite right. For some undocumented reasons in my case new Date() was returning GMT+0200 when new Date(0) was returning GMT+0300 which was right. Since then I always use (new Date(0)).getTimezoneOffset() to get a correct timeshift. ## The Answer 25 -4 people think this answer is useful You just to to include moment.js and jstz.js <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz.min.js"></script>  and after that <script>$(function(){
var currentTimezone = jstz.determine();
var timezone = currentTimezone.name();
});

</script>



-4 people think this answer is useful

This is very good work for me:

// Translation to offset in Unix Timestamp
let timeZoneOffset = ((new Date().getTimezoneOffset())/60)*3600;



-5 people think this answer is useful

you can simply try this. it will return you current machine time

var _d = new Date(), t = 0, d = new Date(t*1000 + _d.getTime())

-5 people think this answer is useful

This will do the job.

var time = new Date(),
timestamp = Date(1000 + time.getTime());
console.log(timestamp);



Thu May 25 2017 21:35:14 GMT+0300 (IDT)



undefined