Next.JS

How to Create Countdown Timer in React Component

How to create a react component for the countdown timer of Next.js under construction page

8/9/2024
0 views
timer.tsxTypeScript
"use client";

import { useEffect, useState } from "react";

interface TimeCount {
  days: string;
  hours: string;
  minutes: string;
  seconds: string;
}

const getTimeLeft = (expiry: string): TimeCount => {
  let days = "0";
  let hours = "0";
  let minutes = "0";
  let seconds = "0";

  const difference = new Date(expiry).getTime() - new Date().getTime();

  if (difference <= 0) {
    return {
      days,
      hours,
      minutes,
      seconds,
    };
  }

  const dys = Math.floor(difference / (1000 * 60 * 60 * 24));
  const hrs = Math.floor((difference / (1000 * 60 * 60)) % 24);
  const mnt = Math.floor((difference / (1000 * 60)) % 60);
  const snd = Math.floor((difference / 1000) % 60);

  days = dys < 10 ? `0${dys}` : dys.toString();
  hours = hrs < 10 ? `0${hrs}` : hrs.toString();
  minutes = mnt < 10 ? `0${mnt}` : mnt.toString();
  seconds = snd < 10 ? `0${snd}` : snd.toString();

  return {
    days,
    hours,
    minutes,
    seconds,
  };
};

const Timer = ({ launchDate }: { launchDate: string }) => {
  const [timeLeft, setTimeLeft] = useState<TimeCount>(getTimeLeft(launchDate));

  useEffect(() => {
    const interval = setInterval(() => {
      setTimeLeft(getTimeLeft(launchDate));
    }, 1000);
  }, [launchDate]);

  return (
    <div className="flex justify-center lg:justify-start mt-10 gap-3">
      <span className="flex flex-col justify-center items-center bg-black text-yellow text-3xl lg:text-5xl w-36 py-3 shadow-lg rounded-lg">
        {timeLeft.days}
        <small className="text-xs lg:text-sm uppercase font-semibold text-white">
          Days
        </small>
      </span>
      <span className="flex flex-col justify-center items-center bg-black text-yellow text-3xl lg:text-5xl w-36 py-3 shadow-lg rounded-lg">
        {timeLeft.hours}
        <small className="text-xs lg:text-sm uppercase font-semibold text-white">
          Hours
        </small>
      </span>
      <span className="flex flex-col justify-center items-center bg-black text-yellow text-3xl lg:text-5xl w-36 py-3 shadow-lg rounded-lg">
        {timeLeft.minutes}
        <small className="text-xs lg:text-sm uppercase font-semibold text-white">
          Minutes
        </small>
      </span>
      <span className="flex flex-col justify-center items-center bg-black text-yellow text-3xl lg:text-5xl w-36 py-3 shadow-lg rounded-lg">
        {timeLeft.seconds}
        <small className="text-xs lg:text-sm uppercase font-semibold text-white">
          Seconds
        </small>
      </span>
    </div>
  );
};

export default Timer;
NextjsReact ComponentCountdown Timer

Loading comments...

Related Examples

How to Create Scrolling Transition on Header Using Next.js 15 and Tailwind CSS
Next.JS

How to Create Scrolling Transition on Header Using Next.js 15 and Tailwind CSS

How to Set Up a Next.js 15 Application: A Step-by-Step Guide
Next.JS

How to Set Up a Next.js 15 Application: A Step-by-Step Guide

How to Deploy a Next.js Application on Vercel
Next.JS

How to Deploy a Next.js Application on Vercel

How to Highlight a Menu on Scroll of the Page Using Next.js and TailwindCSS
Next.JS

How to Highlight a Menu on Scroll of the Page Using Next.js and TailwindCSS

How to Use React Hook Form to Submit a Form in Next.js
Next.JS

How to Use React Hook Form to Submit a Form in Next.js

How to Create a Carousel in Next.js Using Slick-Carousel
Next.JS

How to Create a Carousel in Next.js Using Slick-Carousel

How to Optimize Images in Next.js - Speed Up Your Website
Next.JS

How to Optimize Images in Next.js - Speed Up Your Website

How to Create an Image Gallery using Next.js and TailwindCSS
Next.JS

How to Create an Image Gallery using Next.js and TailwindCSS