Blog / Development
Build a New Year countdown timer in React, time zones included
A countdown to midnight sounds simple until you remember that midnight happens 24 times around the world. Here is a small, accurate React component and the decisions behind it.
By Abdullah NomanDecember 3, 20263 min read

Short answer: decide whether you count down to each visitor's local midnight or to one fixed moment, compute the remaining time from Date.now() on every tick (never by subtracting one second), update once per second, and announce the result politely for screen readers.
Local midnight or one fixed moment?
- Local midnight: every visitor counts to their own New Year. Best for greetings and celebrations.
- Fixed moment: everyone counts to the same instant, such as midnight in New York. Best for a live event or a deadline.
The component
import { useEffect, useState } from 'react';
function nextLocalNewYear(now = new Date()) {
return new Date(now.getFullYear() + 1, 0, 1, 0, 0, 0);
}
export function NewYearCountdown({ target }: { target?: Date }) {
const [end] = useState(() => target ?? nextLocalNewYear());
const [now, setNow] = useState(() => Date.now());
useEffect(() => {
const id = setInterval(() => setNow(Date.now()), 1000);
return () => clearInterval(id);
}, []);
const ms = Math.max(0, end.getTime() - now);
const s = Math.floor(ms / 1000);
const d = Math.floor(s / 86400), h = Math.floor(s % 86400 / 3600),
m = Math.floor(s % 3600 / 60), sec = s % 60;
if (ms === 0) return <p role="status">Happy New Year!</p>;
return (
<>
<p aria-hidden="true">{d}d {h}h {m}m {sec}s</p>
{/* changes once a minute, so screen readers are not flooded */}
<p className="sr-only" aria-live="polite">{`${d} days, ${h} hours, ${m} minutes left`}</p>
</>
);
}
For a fixed moment, pass a UTC instant: new Date('2027-01-01T05:00:00Z') is midnight in New York (UTC−5) on New Year's Day 2027.
Why compute from Date.now()?
Browsers slow down timers in background tabs, so counting down by one second per tick drifts. Calculating the remaining time from the current clock on every tick is always correct when the tab comes back.
Server rendering
In Next.js, the server and browser clocks differ, which causes hydration mismatches. Render a placeholder on the server and start the countdown only in the browser.
Accessibility and motion
- Do not announce every second to screen readers. Above, the visible timer is hidden from them and a separate live region changes only once a minute.
- If you add a flip or pulse animation, turn it off under
prefers-reduced-motion. - Keep the numbers in tabular figures (
font-variant-numeric: tabular-nums) so they do not jump.

At zero
Swap the timer for a greeting and a seasonal illustration, such as fireworks. Keep it small: confetti that covers the page for ten seconds is fun once and annoying on every reload.
Questions
How do I make a countdown to midnight in JavaScript?
Create a Date for January 1 at 00:00 local time and, every second, compute the difference from Date.now().
Why does my countdown timer drift?
Background tabs throttle timers. Compute remaining time from the current clock each tick instead of subtracting one second.
How do I count down to midnight in a specific time zone?
Use a fixed UTC instant, for example 2027-01-01T05:00:00Z for midnight in New York.
How do I avoid hydration errors with a countdown in Next.js?
Render a placeholder on the server and start the countdown in the browser only.


