Countdown Timer Card

Build excitement and urgency for your next event or launch with this dynamic, animated countdown timer card.

This component is perfect for landing pages announcing a new product, a "coming soon" page, or a special promotion with a firm deadline. The timer is powered by lightweight, dependency-free JavaScript.

Screenshot of a card showing a live countdown timer with days, hours, minutes, and seconds.

Get Source Code Preview

About this Component

This countdown timer is a self-contained unit that is easy to configure. The target date and time are set directly in the HTML using a data- attribute. The JavaScript then automatically calculates the remaining time and updates the numbers every second.

Features

Code Breakdown

HTML Structure

The main card div holds a data-target-date attribute. This is where you set the target date in ISO 8601 format (e.g., "2026-01-01T00:00:00"). The timer itself is a container with four segments, one each for days, hours, minutes, and seconds. Each segment has a number span with a unique data-unit attribute (e.g., data-unit="days") and a label span.

JavaScript Logic

The script finds all countdown cards on the page. For each card, it reads the data-target-date attribute to get the target time. It then sets up an interval that runs every 1000 milliseconds (1 second).

Inside the interval function, it:

  1. Calculates the time difference between the current time and the target time.
  2. If the difference is less than zero, it stops the timer (using clearInterval) and displays a completion message.
  3. If time remains, it does the math to convert the total time difference into a number of days, hours, minutes, and seconds.
  4. It then finds the number span for each unit (using card.querySelector('[data-unit="days"]')) and updates its text content. The numbers are padded with a leading zero to maintain a two-digit format.

Code

Here is the complete code. To use it, simply place the components in your project and update the data-target-date attribute in the HTML with your own event's date and time.

View Output