Blog / Build with AI /

Building a countdown timer with AI

A countdown timer sounds trivial until you try to get one right. It’s one of those projects where the idea fits in a sentence and the details bite: time zones, pages left open overnight, a laptop that goes to sleep, a clock that drifts.

That makes it a perfect build-with-AI project. The first version takes minutes. The lessons come when you test it.

The finished thing in one line: a page that counts down to a date and time you choose, showing days, hours, minutes and seconds, and says something sensible when the time arrives.

Who needs one

  • A shop owner counting down to the end of a sale.
  • A blogger counting down to a launch or an event.
  • Anyone who wants a clear “X days to go” banner.

Before you build, a quick honesty check if it’s for a shop: a countdown that resets every time someone visits, creating false urgency, is misleading and can annoy customers. A real deadline is fine. A fake one is not. Build the honest version.

Step 1: the prompt

Write a countdown timer as a single HTML file with inline CSS and JavaScript, no libraries. The target date and time is set in one constant at the top of the script, written as an ISO string with a time zone offset. Show days, hours, minutes and seconds, updating every second. When the time has passed, replace the numbers with the message “It’s time!”. Make it readable on a phone. Add comments.

Spot what’s in there. The “one constant at the top” means you can change the date without hunting through code. The “time zone offset” is the detail most people forget.

Step 2: the core idea

A countdown is subtraction. The browser can tell you the current time and the target time, both as milliseconds. The difference is how long is left. The rest is dividing that number into days, hours, minutes and seconds.

const TARGET = new Date('2026-12-31T23:59:59+03:00');

function update() {
  const ms = TARGET - new Date();
  if (ms <= 0) {
    document.getElementById('timer').textContent = "It's time!";
    clearInterval(timerId);
    return;
  }
  const totalSeconds = Math.floor(ms / 1000);
  const days = Math.floor(totalSeconds / 86400);
  const hours = Math.floor((totalSeconds % 86400) / 3600);
  const minutes = Math.floor((totalSeconds % 3600) / 60);
  const seconds = totalSeconds % 60;
  document.getElementById('timer').textContent =
    days + 'd ' + hours + 'h ' + minutes + 'm ' + seconds + 's';
}

const timerId = setInterval(update, 1000);
update();

The % 86400 part (the remainder after dividing by the number of seconds in a day) is what turns “total seconds” into “hours left today”. If a clock feels like magic, this is the trick: repeated division with remainders.

Calling update() once straight away avoids a one-second blank before the first tick. Ask the AI to explain why clearInterval is there; it’s a nice test of whether you understand what the code does.

Step 3: the time zone trap

This is the lesson. If you write new Date('2026-12-31T23:59:59') with no offset, the browser treats it as the visitor’s local time. A visitor in Lagos and one in Doha would see different deadlines. Sometimes that’s what you want (“midnight wherever you are”). For a sale or a launch, it usually isn’t.

Adding an offset such as +03:00 or Z for UTC fixes the moment in absolute time. Everyone counts down to the same instant. Ask the AI: “Explain what happens if I leave the offset off.” The answer is worth reading.

A small pitfall: a fixed offset doesn’t know about daylight saving. For places that change clocks, using UTC is the least surprising choice.

Step 4: test the awkward cases

Test What to check
Target in the past Message shows at once, no negative numbers
Target five seconds away Reaches zero and stops cleanly
Tab in the background for ten minutes Numbers are right when you return
Laptop asleep, then woken Still correct, because it subtracts from the real clock
Phone in a different time zone setting Same moment as your computer

Background tabs deserve a note. Browsers slow down timers in tabs you’re not looking at, so a naive counter that subtracts one each second would drift. Our version avoids the problem because it recalculates from the real clock every time, rather than counting ticks. That design choice is worth remembering: recompute from the source of truth instead of accumulating small changes.

Step 5: make it friendly

  • Leading zeros. String(n).padStart(2, '0') makes 5 seconds show as 05.
  • Plural words. “1 day”, “2 days”.
  • Accessible announcements. A timer that updates every second shouldn’t be read out every second by a screen reader. Put the visual numbers in an element hidden from assistive tech, and give a calm text such as “Sale ends on 31 December” that doesn’t change. The accessible way of building small widgets is something I covered in building a back-to-top button the accessible way with AI.
  • Reduced motion. If you add flip animations, switch them off for people who prefer reduced motion.

Putting it on a real site

On WordPress, paste the finished HTML into a custom HTML block, or move the script into your theme. On Shopify, a section or a custom liquid block works. Keep the script small and test on a phone, since a countdown near the top of a page is one of the first things visitors see.

One more caution: if the page is cached, the target date is cached too. Change the date only in one place, and clear the cache when you do.

Key takeaways

  • A countdown is a subtraction between a target and the real clock.
  • Always include a time zone offset, or use UTC, for shared deadlines.
  • Recompute from the clock each tick; don’t count ticks.
  • Use real deadlines only.
  • Test past dates, background tabs and sleep.

FAQ

Why does my timer jump or freeze in a background tab?

Browsers throttle timers in hidden tabs. Because ours recalculates from the clock, it catches up when you return.

Can I make it repeat weekly?

Yes, but you’d calculate the next occurrence in code. It’s a good follow-up prompt.

Is it fine to use a plugin instead?

Often, yes. Build your own when you want a lighter page or the practice.

What would you count down to, and does it have a real deadline?