Wednesday, April 24, 2024
HomePythonThe best way to get present time zone in JavaScript

The best way to get present time zone in JavaScript


You will get your time zone utilizing JavaScript through the use of the Intl.DateTimeFormat object which is offered in all trendy browsers and returns the language-specific date and time formatting strategies.

Right here is the way it works. Merely open the developer console in your internet browser and run the next code:

const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(timezone); 
// Africa/Casablanca

That is the precise time zone however you can even get the offset in minutes of your timzeone utilizing getTimezoneOffset() technique.

Get timezone offset in minutes

The getTimezoneOffset() technique of the JavaScript Date object can be utilized to acquire the time zone of the net browser.

The getTimezoneOffset() operate provides you the offset, in minutes, from Coordinated Common Time to your present time zone.

Optimistic is returned if the native time zone is later than UTC, and unfavorable whether it is earlier. The getTimezoneOffset() is offered in all trendy browsers.

Let’s take the next instance:

const date = new Date();
console.log(date.getTimezoneOffset());    // -60

The distinction between my time zone and UTC is 60 minutes which implies one hour.

Please word that Daylight Saving Time (DST) causes the returned worth to fluctuate all year long.

The results of a name to getTimezoneOffset() for a time zone that transitions into and out of Daylight Saving Time (DST) every year can fluctuate by as much as 60 minutes.

In the identical collection:

The best way to get present time zone in JavaScript The best way to add days to dates in JavaScript

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments