How to Detect User Timezone in JavaScript?


To detect the user timezone with the name of the timzone itself, use the Internationalization API. This gives the name of the Timezone in which the user and the browser is being worked on.

Detect Exact Timezone with Name

Example

To get the exact timezone name, we will use the Internationalization API −

<!DOCTYPE html> <html> <body> <h1>Timezone</h1> <p id="test"></p> <script> document.getElementById("test").innerHTML = Intl.DateTimeFormat().resolvedOptions().timeZone; </script> </body> </html>

Output


Get the Timezone (Difference between UTC and local time)

Example

To get the timezone, use the getTimezoneOffset() in JavaScript. This method returns the time difference between UTC time and local time. The difference returned is in minutes.

<!DOCTYPE html> <html> <body> <h1>Timezone</h1> <p id="test"></p> <script> const dt = new Date(); let diffTZ = dt.getTimezoneOffset(); document.getElementById("test").innerHTML = diffTZ; </script> </body> </html>

Output

Updated on: 13-Sep-2023

32K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements