🍋
Menu
Troubleshooting Beginner 2 min read 307 words

Time Zone Math and Date Calculations Explained

Time zone conversions, daylight saving transitions, and date arithmetic are notoriously error-prone. This guide covers the rules and edge cases that trip up even experienced developers.

Key Takeaways

  • Coordinated Universal Time (UTC) is the zero-offset reference point.
  • DST shifts clocks forward (spring) and backward (fall), changing the UTC offset temporarily.
  • Convert both dates to a day count (Julian Day Number or Unix timestamp) and subtract.
  • The international standard for date/time strings:
  • Assuming all days have 24 hours (DST days have 23 or 25)

UTC as the Universal Reference

Coordinated Universal Time (UTC) is the zero-offset reference point. All time zones are expressed as offsets from UTC: EST is UTC-5, JST is UTC+9.

To convert between zones: subtract the source offset and add the target offset.

Example: 3:00 PM EST (UTC-5) to JST (UTC+9): 15:00 + 5 (to UTC) + 9 (to JST) = 29:00 = 05:00 next day

Daylight Saving Time

DST shifts clocks forward (spring) and backward (fall), changing the UTC offset temporarily. Not all regions observe DST, and transition dates vary by country:

Region Spring Forward Fall Back
US/Canada 2nd Sunday March 1st Sunday November
EU Last Sunday March Last Sunday October
Australia 1st Sunday October 1st Sunday April

During the spring transition, 2:00 AM jumps to 3:00 AM — the hour from 2:00 to 2:59 does not exist. In fall, 1:00-1:59 AM occurs twice.

Date Arithmetic

Days Between Dates

Convert both dates to a day count (Julian Day Number or Unix timestamp) and subtract. Account for leap years: a year is leap if divisible by 4, except centuries unless divisible by 400.

Adding Months

Adding 1 month to January 31 creates an ambiguous result (February 31 does not exist). Libraries typically clamp to the last valid day of the target month (February 28/29).

ISO 8601 Format

The international standard for date/time strings: 2026-03-10T14:30:00+09:00

Always store datetimes in UTC and convert to local time zones for display. This prevents ambiguity and simplifies calculations.

Common Mistakes

  • Assuming all days have 24 hours (DST days have 23 or 25)
  • Hardcoding UTC offsets instead of using timezone database names (America/New_York)
  • Calculating age by subtracting years without checking if the birthday has passed this year