Date.prototype.getTimezoneOffset()
The only Date method whose whole purpose is to describe the runtime. Its sign convention is inverted relative to ISO 8601, and its value depends on the date because of daylight saving.
Demo
This number is YOURS — it describes wherever you are reading this from, so no two readers necessarily see the same output. If you are in UTC it is 0; west of Greenwich it is positive; east it is negative. Compare the summer and winter cases: if they differ, your region observes daylight saving, and the offset is a property of the DATE as much as of the place. That is why caching a single offset and applying it to other dates is wrong.
Common patterns
const o = -d.getTimezoneOffset(); const sign = o >= 0 ? '+' : '-';
Intl.DateTimeFormat().resolvedOptions().timeZone;
d.toLocaleString('en-GB', {timeZone: 'Asia/Tokyo'});
Examples
Pitfalls
new Date().getTimezoneOffset()
-new Date().getTimezoneOffset()
const OFFSET = new Date().getTimezoneOffset();
const offset = someDate.getTimezoneOffset();
Math.trunc(-330 / 60)
`${Math.trunc(330 / 60)}:${330 % 60}`
save({offset: -120})
save({tz: Intl.DateTimeFormat().resolvedOptions().timeZone})
When to use
- Reporting or logging the runtime offset for diagnostics
- Building an ISO offset suffix, with the sign negated
- Detecting whether the runtime is in UTC
- Converting between timezones → Intl with a timeZone option
- Storing a user timezone → the IANA zone name
- Assuming whole hours → some zones are offset by 30 or 45 minutes
- Caching the value → it changes across DST boundaries
Notes
FAQ
Because the method is defined as the adjustment needed to get from local time TO UTC, not the offset of local time from UTC. Those are opposites. ISO 8601 writes +02:00 for a zone that this method reports as −120.
const isoOffsetMinutes = -d.getTimezoneOffset();