An Astro countdown timer is a component that takes a target date as a prop, renders the date itself as a <time> element (so the page still makes sense without JavaScript), and runs a small script that updates days, hours, minutes and seconds every second. Because Astro pages are usually built ahead of time, the live numbers must be calculated in the browser, not in the component frontmatter.
Key Takeaways
- Calculate remaining time in the browser; a static build would freeze the numbers at build time.
- Render the target date in a
<time datetime>element as the fallback.- Store the target in a
data-attribute so one script powers any number of timers.- Don’t announce every tick to screen readers.
The component
---
// src/components/Countdown.astro
interface Props {
to: string | Date; // e.g. "2026-12-01T09:00:00Z"
label?: string;
doneText?: string;
}
const { to, label = 'Launches in', doneText = 'We are live' } = Astro.props;
const target = new Date(to);
const readable = target.toLocaleString('en-US', { dateStyle: 'long', timeStyle: 'short', timeZone: 'UTC' });
---
<div class="countdown" data-countdown={target.toISOString()} data-done={doneText}>
<p class="label">{label}</p>
<p class="fallback"><time datetime={target.toISOString()}>{readable} UTC</time></p>
<p class="clock" aria-hidden="true" hidden>
<span data-unit="d">0</span>d <span data-unit="h">00</span>h
<span data-unit="m">00</span>m <span data-unit="s">00</span>s
</p>
</div>
<script>
const pad = (n: number) => String(n).padStart(2, '0');
document.querySelectorAll<HTMLElement>('[data-countdown]').forEach((el) => {
const target = new Date(el.dataset.countdown!).getTime();
const clock = el.querySelector<HTMLElement>('.clock')!;
const unit = (u: string) => el.querySelector<HTMLElement>(`[data-unit="${u}"]`)!;
el.querySelector<HTMLElement>('.fallback')!.hidden = true;
clock.hidden = false;
const tick = () => {
const left = Math.max(0, target - Date.now());
const s = Math.floor(left / 1000);
unit('d').textContent = String(Math.floor(s / 86400));
unit('h').textContent = pad(Math.floor((s % 86400) / 3600));
unit('m').textContent = pad(Math.floor((s % 3600) / 60));
unit('s').textContent = pad(s % 60);
if (left === 0) {
clearInterval(timer);
clock.textContent = el.dataset.done!;
clock.removeAttribute('aria-hidden');
}
};
const timer = setInterval(tick, 1000);
tick();
});
</script>
<style>
.clock { font-variant-numeric: tabular-nums; font-size: 2rem; font-weight: 700; }
</style>
Use it on a launch page:
---
// src/pages/index.astro
import Countdown from '../components/Countdown.astro';
---
<Countdown to="2026-12-01T09:00:00Z" label="Doors open in" />
Why the script recalculates from the target
The script computes target - Date.now() on every tick instead of subtracting one second from a counter. Browsers throttle timers in background tabs, so a counter drifts; recalculating from the clock is always correct when the tab comes back.
font-variant-numeric: tabular-nums gives every digit the same width, so the numbers don’t wobble as they change.
Time zones
Write the target as an ISO string with a Z or an offset (2026-12-01T09:00:00-05:00). A bare 2026-12-01 09:00 is interpreted in whatever time zone the build machine or the visitor’s browser uses, and the two can disagree by hours. The fallback text states the time in UTC so it’s never ambiguous.
Accessibility
The ticking clock is marked aria-hidden="true", because a screen reader announcing a new number every second is unusable. The visible label and, before JavaScript runs, the <time> element give assistive technology the meaningful information: when the event happens. When the countdown ends, the done text becomes readable.
Several timers and view transitions
The script loops over every [data-countdown] element, so a page with three timers needs no extra code. Astro bundles the script once per page. If you use <ClientRouter />, move the loop into a function and call it on astro:page-load; see re-running scripts after view transitions.
When you’d rather not write it at all, our Launch & Coming Soon themes include countdown and waitlist layouts. For how components and scripts fit together, see the Astro components guide and the pages docs.
FAQ
Why does my Astro countdown show the wrong time?
If you calculate the remaining time in the component frontmatter, it runs at build time on a static site and never updates. Calculate it in a client-side script from the target date.
Do I need React for a countdown in Astro?
No. A plain <script> in an Astro component is enough and ships far less JavaScript than a framework island.
How do I hide the countdown after the date passes?
Check left === 0 in the script and replace or hide the element. On a server-rendered page you can also compare dates in the frontmatter and skip rendering the timer entirely.