Skip to content

Timezone Offset Miscalculation

Quick summary

You combined getHours (local) with getUTCHours, or forgot the sign on getTimezoneOffset — the resulting time is off by hours.

Why this happens

  • Using getHours for a value that should be UTC (or vice versa)

  • Forgetting that getTimezoneOffset returns minutes with a flipped sign

  • Storing local time strings but treating them as UTC when displaying

Minimal example

✗ Broken code
const d = new Date('2023-06-15T12:00:00Z');
const localHour = d.getHours(); // varies by zone
const utcHour = d.getUTCHours(); // 12
console.log(localHour + utcHour); // mixing zones — meaningless
✓ Fixed code
const d = new Date('2023-06-15T12:00:00Z');
const utcHour = d.getUTCHours(); // 12
const utcMinute = d.getUTCMinutes(); // 0
console.log(`UTC: ${utcHour}:${utcMinute}`); // consistent UTC

Mixing getHours (local) with getUTCHours (UTC) combines values from different frames of reference. Using only UTC methods keeps the computation consistent.

How to diagnose

  • Print the date in both local and UTC formats to see the gap

  • Check every get* vs getUTC* call for consistency

  • Compare the computed offset with the known timezone difference

How to fix

  • Use only UTC methods (getUTC*) when the value is UTC, or only local methods when local

  • Use a library (dayjs, date-fns-tz) that handles timezone math explicitly

  • Store and transmit dates in ISO 8601 with offset, then convert at display time

How to prevent

  • Pick one convention (UTC or local) and stick to it across the codebase

  • Use a timezone-aware library for any cross-zone arithmetic

Related Resources

Related Glossary

Related Lessons

← Back to language