Skip to main content
Lasita
Menu

Dates & Calendars · 9 min read

Date Math Without DST Bugs: Days, Working Days and Leap Years

Why dividing JavaScript timestamps by 86,400,000 fails across Daylight Saving Time transitions, and how Howard Hinnant civil-day integer math solves days-between, O(1) working days, month-end clamping, and leap birthdays cleanly.

By

Published

Key takeaways

  • Dividing millisecond differences by 86,400,000 produces off-by-one errors across 23-hour spring-forward and 25-hour fall-back DST days.
  • Howard Hinnant’s days_from_civil / civil_from_days algorithm represents dates as pure serial integers with zero timezone or DST drift.
  • Explicit policies for inclusive end dates (+1 day), month-end clamping, custom regional weekends, and Feb 29 birthdays prevent ambiguous scheduling errors.
In this guide
  1. 1. Why Millisecond Date Math Breaks on DST Transitions
  2. 2. Pure Integer-Day Arithmetic (days_from_civil)
  3. 3. O(1) Working Days, Regional Weekends & Holiday Deduplication
  4. 4. Edge Cases: Month-End Clamping, Feb 29 Birthdays & Pre-1582 Dates
  5. 5. Inclusive (+1 Day) vs Exclusive Day Counting in Contracts

Why Millisecond Date Math Breaks Across DST Transitions

Counting the days between two calendar dates sounds like one of the simplest problems in software engineering — yet naive implementations fail silently twice every year across North America, Europe, and parts of the Southern Hemisphere.

The root cause is confusing civil calendar dates (e.g., 2026-03-08) with physical UTC timestamps (milliseconds since 1970-01-01T00:00:00Z). Most calendar days contain 24 hours (86,400,000 ms), but Daylight Saving Time (DST) transitions create 23-hour spring-forward days and 25-hour fall-back days:

// CLASSIC BUG: Dividing milliseconds by 86,400,000 fails across DST!
// In America/New_York, 2026-03-08 is a 23-hour "spring forward" day:
const start = new Date(2026, 2, 8);  // Mar 8, 2026 00:00 local
const end   = new Date(2026, 2, 9);  // Mar 9, 2026 00:00 local

const diffDays = Math.floor((end.getTime() - start.getTime()) / 86_400_000);
// Result: 0 days instead of 1 day (only 23 hours elapsed = 0.9583 days)!

Worse, the exact same JavaScript code returns 1 in UTC and Asia/Kolkata (which does not observe DST) while returning 0 in America/New_York! Even adding new Date(2026, 0, 31) + 1 month behaves unpredictably, rolling Jan 31 forward to Mar 3 instead of clamping cleanly to Feb 28.

Millisecond math gives 0 days across the 8 March 2026 DST change while integer day math gives 1 day Sun 8 Mar 2026 → Mon 9 Mar 2026 (America/New_York) 2→3 only 23 hours of local time Millisecond math ✗ diff = 82,800,000 ms diff / 86,400,000 = 0.958 Math.floor(0.958) = 0 days Integer day math ✓ 8 Mar 2026 = day 20520 9 Mar 2026 = day 20521 20521 − 20520 = 1 day
Figure 1. In New York, clocks jump from 2:00 to 3:00 on 8 March 2026, so local midnight to midnight is only 23 hours. Dividing the millisecond difference by 86,400,000 and rounding down reports 0 days. Counting whole calendar days as integers (days since 1970-01-01) gives the correct answer, 1.

Pure Integer-Day Arithmetic (days_from_civil)

The cleanest engineering solution — used throughout Lasita Date & Days Calculator — is to eliminate JavaScript Date arithmetic completely and represent every calendar date as a single pure integer day count using Howard Hinnant’s celebrated days_from_civil / civil_from_days formulas:

// Howard Hinnant's pure integer civil-day algorithm (zero Date / DST drift)
function daysFromCivil(year: number, month: number, day: number): number {
  const y = month <= 2 ? year - 1 : year;
  const era = Math.floor(y >= 0 ? y / 400 : (y - 399) / 400);
  const yoe = y - era * 400;                                      // [0, 399]
  const doy = Math.floor((153 * (month + (month > 2 ? -3 : 9)) + 2) / 5) + day - 1;
  const doe = yoe * 365 + Math.floor(yoe / 4) - Math.floor(yoe / 100) + doy;
  return era * 146097 + doe - 719468; // 0 == 1970-01-01
}

// Exact day count between ANY two Gregorian calendar dates:
// daysFromCivil(2026, 3, 9) - daysFromCivil(2026, 3, 8) === 1 (in EVERY timezone!)

Why does this algorithm work so reliably?

  • Zero timezone dependence: Because only pure integers (year, month, day) enter the equation, results are mathematically identical in UTC, America/New_York, Asia/Kolkata (+05:30), Pacific/Auckland, and Pacific/Chatham (+12:45/+13:45).
  • Exact Gregorian leap-year rules: A year is a leap year if divisible by 4, except centuries not divisible by 400 (so 2000 was a leap year, 1900 and 2100 are not). Shifting the internal calculation year to start on March 1 places the leap day (February 29) at the very end of the year, removing conditional branches cleanly.
  • O(1) weekday lookup: Once you have serial day z, the day of the week is simply ((z + 4) % 7 + 7) % 7 (where 0 = Sunday, 1 = Monday, …, 6 = Saturday).

O(1) Working Days, Regional Weekends & Holiday Deduplication

Naive business-day calculators loop day by day from start to end checking date.getDay(). That works for a 30-day sprint, but becomes slow over 50-year ranges and hardcodes Saturday + Sunday as the only weekend globally.

In reality, business weeks vary internationally:

Weekend Pattern Common Regions Working Days / Week
Saturday + Sunday Americas, Europe, India, Australia, UAE 5 days
Friday + Saturday Saudi Arabia, Egypt, Jordan, Israel, Bangladesh 5 days
Sunday only 6-day commercial, manufacturing & retail schedules 6 days
Custom 7-day mask 4-day workweeks, shift rotations, hospital rosters 1–6 days (configurable)

With integer-day arithmetic, counting working days between any two dates takes O(1) constant time: split the span into fullWeeks = Math.floor(totalDays / 7) (each contributing the exact count of active workdays in the 7-day bitmask) plus at most 6 remainder days checked directly against the mask. Then subtract only unique holidays that actually fall inside the range on a non-weekend day — preventing double-subtraction when a holiday lands on a weekend or appears twice in a pasted list. Try this live in Lasita Working Days Calculator.

Edge Cases: Month-End Clamping, Feb 29 Birthdays & Pre-1582 Dates

Reliable date software surfaces edge cases explicitly instead of silently guessing:

  • Month-end clamping: What is January 31 + 1 month? JavaScript overflows into March 3. Financial and scheduling standards clamp to the last valid day of the target month (February 28, or February 29 in a leap year) and show an explicit clamping notice so contracts stay unambiguous. Explore this in Add / Subtract Days.
  • Leap-year birthdays (Feb 29): People born on February 29 celebrate their legal/personal birthday in non-leap years on either February 28 or March 1 depending on jurisdiction. Lasita Exact Age Calculator lets you switch explicitly between both rules.
  • Pre-1582 historical dates: The Gregorian calendar was introduced in October 1582. Extending Gregorian leap rules backward before 1582 produces the proleptic Gregorian calendar (ISO 8601 standard), which differs from historical Julian records — a clear notice prevents accidental historical misinterpretation.

Inclusive (+1 Day) vs Exclusive Day Counting in Contracts

One of the most frequent real-world discrepancies in travel bookings, project timelines, and notice periods is whether the final day counts:

  • Exclusive (standard subtraction): Checking into a hotel on Monday and checking out on Thursday is 4 − 1 = 3 nights.
  • Inclusive (+1 day): Taking paid leave from Monday through Thursday inclusive covers 4 calendar days (Monday, Tuesday, Wednesday, and Thursday).

Always confirm whether an SLA, notice period, or leave request is inclusive or exclusive. On dates.lasita.dev/days-between-dates, an explicit Include end date (+1 day) toggle updates day counts, week breakdowns, and weekday splits transparently — and works completely offline as an installable PWA.

About the author

Abishek Verma

Software engineer & writer behind Lasita · India

Abishek Verma builds and maintains every Lasita tool and writes each guide on this site. Lasita began as a simple idea: everyday file, developer and date utilities should work entirely inside your browser, so nothing you open in them has to be uploaded to someone else’s server.

More about Lasita Email contact@lasita.dev

Spotted a mistake or have a question about this guide? Email contact@lasita.dev.

All 8 guides →