Dev.to WebDev 🛠 Dev 👁 2 📖 3 min read

Counting Down to a Fixed Date in Local Time: Four Timezone Traps Behind a One-Line Countdown

Every year, around this time, search volume for "how many days until Valentine's Day" spikes, and every year a large share of the answers are off by one for somebody. It looks like a one-liner problem: const days = Ma

Every year, around this time, search volume for "how many days until Valentine's Day" spikes, and every year a large share of the answers are off by one for somebody. It looks like a one-liner problem:

const days = Math.ceil((target - new Date()) / 86_400_000);

That line is wrong in at least four different ways. I build daysuntilvalentinesday.com, a small countdown page, and these are the traps I actually hit while making the number correct for visitors in every timezone.

1. "How many days" is a calendar question, not a duration question

(target - now) / 86400000 gives an elapsed duration in days. Users do not want that. If it is 23:00 on February 12 in Tokyo, the honest answer is "1 day", even though only about 12 hours remain until local midnight on the 14th. If it is 01:00 on February 14, the answer is "0 days, it is today".

So the calculation has to happen on calendar dates in the visitor's timezone, not on timestamps:

function daysBetweenLocalDates(now, targetMonth, targetDay, timeZone) {
  const fmt = new Intl.DateTimeFormat('en-CA', {
    timeZone, year: 'numeric', month: '2-digit', day: '2-digit',
  });
  const [y, m, d] = fmt.format(now).split('-').map(Number);

  const today = Date.UTC(y, m - 1, d);
  let year = y;
  // the target may already have passed this year: roll forward
  const target = Date.UTC(year, targetMonth - 1, targetDay);
  const next = today <= target ? target : Date.UTC(year + 1, targetMonth - 1, targetDay);

  return Math.round((next - today) / 86_400_000);
}

The trick is Date.UTC on the date parts only. Once both sides are midnight-UTC of a calendar date, the subtraction is exactly n * 86400000 regardless of DST, and rounding is safe.

2. DST breaks any assumption about day length

Doing the calibration in local time (new Date(y, m - 1, d)) looks cleaner and is a trap: on a spring-forward day that local midnight is 23 hours after the previous one, so a difference of 3 calendar days can measure 71 hours and Math.ceil silently becomes 4. Calendar arithmetic in UTC and timezone-aware formatting for display is the only combination I found that stays correct across DST boundaries, half-hour offsets (Asia/Kolkata) and 45-minute ones (Asia/Kathmandu).

3. The target instant is local, not UTC

new Date('2027-02-14T00:00:00Z') is February 13 for anyone west of Greenwich. If the countdown is meant for the visitor's own midnight, never construct the target from a UTC string; construct it from the target month and day and let Intl.DateTimeFormat decide what "today" means locally.

4. February 29 exists

If the target ever lands on a leap day, Date.UTC(year, 1, 29) in a non-leap year silently becomes March 1. I validate the round-trip (getUTCMonth() === expectedMonth) and clamp instead of trusting the constructor.

The part nobody warns you about: the calendar file

A countdown page is a five-second visit. The useful artifact is a recurring .ics entry so the reminder arrives every year without anyone remembering the URL. Two details matter:

BEGIN:VEVENT
DTSTART;VALUE=DATE:20270214
DTEND;VALUE=DATE:20270215
RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=14
SUMMARY:Valentine's Day
END:VEVENT
  • All-day events use VALUE=DATE with an exclusive DTEND; a one-day event therefore ends on the 15th.
  • FREQ=YEARLY with BYMONTH/BYMONTHDAY is more robust than INTERVAL=1, because some clients anchor recurrence on the original weekday.
  • No VTIMEZONE block for all-day events — a calendar date is already timezone-free, and adding a TZID is how you get off-by-one reminders.

What I actually ship

The site is deliberately small, but each piece comes from one of the bugs above: a live timer (so the answer is not stale after a page is left open), the number of weekends left (people plan around weekends, not days), a yearly .ics download for calendar alerts, and a four-week planning checklist. Everything is client-side and timezone-aware — the countdown recomputes when a visitor's timezone or system clock changes rather than trusting the value baked in at build time.

If you are building anything date-relative — an advent calendar, a tax deadline page, a "days until launch" widget — the ordering that saved me the most bug reports was: compute in calendar dates, render in the visitor's timezone, and never trust a duration to mean a number of days.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.