w3docs logo
  1. Snippets
  2. JavaScript
  3. How to Get the Client Timezone Offset in JavaScript

How to Get the Client Timezone Offset in JavaScript

The client’s timezone offset can be detected by some JavaScript methods. Let’s discuss each of them in this tutorial.

getTimezoneOffset()

You can get the client’s timezone offset by using the Date object’s getTimezoneOffset() method:

w3docs logo Javascript Date object getTimezoneOffset() method
let offset = new Date().getTimezoneOffset(); console.log(offset);

toString()

The method of toString() can be useful if you want the client timezone to be formatted:

w3docs logo Javascript Date object toString method
let split = new Date().toString().split(" "); let timeZoneFormatted = split[split.length - 2] + " " + split[split.length - 1].slice(0,4); console.log(timeZoneFormatted);

Intl.DateTimeFormat()

You can also use another method to get the system's IANA timezone in JavaScript:

w3docs logo Javascript Date time format method
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);

In the ECMAScript 2015 Internationalization API version, 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. in previous versions, it will return undefined.

This method is supported nearly on all browsers.

The Intl.DateTimeFormat constructor is for objects enabling language-sensitive date and time formatting.

The getTimezoneOffset() Method

The getTimezoneOffset() method represents the time difference between the UTC time and the local time, in minutes. It returns non-constant value because of the practice of using Daylight Saving Time.

The toString() Method

The toString() method returns a string representing the specified Date object. Date instances inherit their toString() method from Date.prototype. The Date.prototype.toString() returns a string representation of the Date in the format that is specified in ECMA-262.

Do you find this helpful?