Get the IANA Timezone
We'll invoke the
Intl.DateTimeFormat() method from the Internationalization API to get the client's timezone:
The ECMAScript 2015 update introduced this method, and it currently works in 93.75% of browsers globally.
Get the Timezone Offset
To get the client's timezone offset, we'll use the
getTimezoneOffset() method on a
One thing to note is not all timezones are returned in whole hours. Because of this, the timezone offset returns in minutes instead of hours. To convert the offset into hours, we can perform a simple calculation by dividing the client's timezone in minutes by 60, the number of minutes in an hour:
console.log(new Date().getTimezoneOffset() / 60);
There are no comments yet. Start the conversation!