get client timezone offset in javascript

How to Get Client Timezone & Offset in JavaScript

Sometimes you may need to get client timezone and offset using JavaScript, on web page. It is sometimes required in case you want to provide location/timezone based personalization. But this can be complicated since these values may change due to instances like daylight savings. In this article, we will learn how to correctly get client timezone & offset in JavaScript.

How to Get Client Timezone & Offset in JavaScript

There are plenty of ways to do this. There are also many third party libraries for this purpose.

1. How to Get Timezone

Typically, developers use time zone offset to calculate timezone but it can give erroneous results since these offset values can change from time to time, such as daylight savings. Instead, you can get IANA (Internet Assigned Network Authority) timezone of your client using Intl object as shown below.


2. How to Get Offset

Timezone offset is the difference in minutes (positive or negative) between UTC (Universal Coordinated Time) and your local time. If the offset is positive, it means that your local timezone is behind UTC while negative offset means it is ahead of UTC. You can get timezone offset for your client using getTimezoneOffset() function.

var offset = new Date().getTimezoneOffset();
// if offset equals -60 then the time zone offset is UTC+01

In this article, we have learnt how to get client timezone and offset, calculated in web browser using JavaScript.

Also read:

How to Check if JS Object has Property
How to Auto Increment With Prefix As Primary Key in MySQL
How to Set Initial Value & Auto Increment in MySQL
How to Create DMARC Record for your Domain
MySQL Query to Get Column Names from Table

Leave a Reply

Your email address will not be published.