To get the current browser's time zone, you can use the getTimezoneOffset()
method from the JavaScript Date
object.
The getTimezoneOffset()
returns the time difference, in minutes, between UTC time and local time. The returned value is positive if the local time zone is behind UTC and negative if the local time zone is ahead of UTC.
For example, if your time zone is UTC+5, the getTimezoneOffset()
method will return -300
minutes:
const date = new Date();
const offset = date.getTimezoneOffset();
console.log(offset); // -300
The getTimezoneOffset()
works in all modern browsers, Internet Explorer 5 and higher.
Daylight Saving Time (DST) time zones
Note that the returned value is not always constant due to Daylight Saving Time (DST) time zones.
For a time zone that moves in and out of Daylight Saving Time (DST) every year, the number of offset minutes returned by calling getTimezoneOffset()
can vary +/- 60 minutes.
For example, North America uses Pacific Standard Time (PST) during wintertime and Pacific Daylight Time (PDT) during summertime. The PST time is 8 hours behind UTC (UTC-08:00
), whereas the PDT time is 7 hours behind UTC (UTC-07:00
).
Current time | PST (UTC-08:00) | PDT (UTC-07:00) |
---|---|---|
Return value | 480 | 420 |
For non-DST time zones, the getTimezoneOffset()
method always returns the same number of minutes.
Time zone != offset
The getTimezoneOffset()
method only gives you the local time zone offset from UTC time and not the actual time zone (e.g. Europe/Berlin
).
Using an offset to calculate time zone is not always accurate due to daylight saving rules. Instead, you should use the Intl.DateTimeFormat
object.
The Intl.DateTimeFormat
object is available in all modern browsers and provides the language-specific date and time formatting methods.
Here is an example that shows how you can get the system's IANA time zone in JavaScript:
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(timezone); // Asia/Karachi
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.