Home JavaScript How to Create an Animated JavaScript Clock Using HTML, CSS...

How to Create an Animated JavaScript Clock Using HTML, CSS & JS

Share

Looking for a modern and responsive JavaScript animated clock for your website? In this tutorial, we will create a beautiful Responsive JavaScript Animated Clock using HTML, CSS, and JavaScript. This project features a stylish analog clock, a digital time display, smooth rotating clock hands, and a live date display.

The clock comes with a modern dark UI design, glowing blue and purple effects, and a fully responsive layout that works on desktop, tablet, and mobile devices.

Whether you are a beginner or an experienced web developer, this project will help you understand how to build interactive clock animations using JavaScript.

Features of JavaScript Animated Clock

Here are the main features included in this animated clock project:

  • Responsive Design: Automatically adjusts to desktop, tablet, and mobile screens.
  • Animated Analog Clock: Displays hour, minute, and second hands with smooth movement.
  • Digital Clock: Shows the current time in real time.
  • 12/24-Hour Format: Easily switch between 12-hour and 24-hour time formats.
  • Live Date Display: Shows the current weekday, date, month, and year.
  • Neon UI Design: Modern dark background with glowing blue and purple effects.
  • CSS Animations: Smooth glowing border effects and interactive button animations.
  • Pure JavaScript: No additional JavaScript libraries or frameworks required.

Technologies Used

This project uses three main web development technologies:

  • HTML5 – Creates the clock structure and elements.
  • CSS3 – Handles responsive layouts, styling, glowing effects, and animations.
  • JavaScript – Updates the clock hands, digital time, and date dynamically.

How Does the Animated Clock Work?

The analog clock uses JavaScript’s Date object to get the current hours, minutes, and seconds. These values are converted into rotation angles and applied to the clock hands using CSS transforms.

The requestAnimationFrame() method provides smooth animation by continuously updating the clock. JavaScript also generates the clock numbers and minute markers dynamically, making the design easier to maintain.

The digital clock displays the current time, while the date section automatically updates according to the user’s local system date.

Responsive JavaScript Animated Clock – Source Code

Below, you can use the complete HTML, CSS, and JavaScript source code to create your own animated clock.

Simply copy the code, save it as responsive-animated-clock.html, and open the file in your browser. You can customize the background colors, clock size, glowing effects, fonts, and animations according to your website design.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive JavaScript Animated Clock Code By Mycodingtutorial</title>

<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    min-height: 100vh;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: #05091b;
    font-family: 'Inter', sans-serif;
    color: #fff;
}

/* Main Container */

.clock-container {
    width: 100%;
    max-width: 480px;
    padding: clamp(20px, 5vw, 35px);
    text-align: center;
    position: relative;
    overflow: hidden;
    border: 1px solid #1649a7;
    border-radius: 25px;
    background: linear-gradient(145deg,#071a3d,#080b20);
    box-shadow: 0 20px 60px #0008,
                0 0 35px #006aff20;
}

.clock-container::before {
    content: "";
    position: absolute;
    width: 200px;
    height: 200px;
    top: -130px;
    left: -80px;
    background: #9c27ff;
    filter: blur(90px);
    opacity: .35;
    pointer-events: none;
}

.clock-container::after {
    content: "";
    position: absolute;
    width: 200px;
    height: 200px;
    bottom: -130px;
    right: -80px;
    background: #007bff;
    filter: blur(90px);
    opacity: .35;
    pointer-events: none;
}

.clock-content {
    position: relative;
    z-index: 1;
}

/* Heading */

.clock-heading {
    font-size: clamp(12px, 3vw, 15px);
    font-weight: 800;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: #53baff;
    margin-bottom: 30px;
}

/* Analog Clock */

.clock {
    width: min(320px, 100%);
    aspect-ratio: 1 / 1;
    margin: auto;
    position: relative;
    border-radius: 50%;
    border: 5px solid #137dff;

    background:
        radial-gradient(circle,#092653 0%,#061632 70%,#02091c 100%);

    box-shadow:
        0 0 10px #00aaff,
        0 0 25px #006aff,
        0 0 50px #792bff66,
        inset 0 0 30px #006aff30;
}

.clock::before {
    content: "";
    position: absolute;
    inset: -9px;
    border-radius: 50%;
    border: 3px solid transparent;
    border-top-color: #00eaff;
    border-right-color: #a52cff;
    filter: drop-shadow(0 0 8px #00aaff);
    animation: borderGlow 4s linear infinite;
}

@keyframes borderGlow {
    to {
        transform: rotate(360deg);
    }
}

/* Numbers */

.number {
    position: absolute;
    font-size: clamp(17px, 5vw, 25px);
    font-weight: 700;
    color: #fff;
    transform: translate(-50%,-50%);
    text-shadow: 0 0 10px #fff8;
    line-height: 1;
}

/* Clock Markers */

.tick {
    position: absolute;
    width: 2px;
    height: 7px;
    background: #228cff;
    left: 50%;
    top: 50%;
    transform-origin: center;
}

.tick.major {
    width: 4px;
    height: 13px;
    background: #fff;
    box-shadow: 0 0 8px #00aaff;
}

/* Clock Hands */

.hand {
    position: absolute;
    bottom: 50%;
    left: 50%;
    transform-origin: bottom center;
    transform: translateX(-50%) rotate(0deg);
    border-radius: 20px;
    z-index: 3;
}

.hour {
    width: 8px;
    height: 24%;
    background: #fff;
    box-shadow: 0 0 12px #fff;
}

.minute {
    width: 5px;
    height: 34%;
    background: #13c1ff;
    box-shadow: 0 0 15px #00aaff;
}

.second {
    width: 3px;
    height: 39%;
    background: #ff315d;
    box-shadow: 0 0 12px #ff315d;
}

/* Center */

.center {
    position: absolute;
    width: 21px;
    height: 21px;
    background: #fff;
    border: 5px solid #ff315d;
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    z-index: 5;
    box-shadow: 0 0 15px #ff315d;
}

/* Digital Clock */

.digital-box {
    width: 100%;
    margin-top: 32px;
    padding: 15px 8px;
    border: 2px solid #078aff;
    border-radius: 17px;
    background: #030d27;
    box-shadow: 0 0 20px #007bff55,
                inset 0 0 15px #006aff20;
}

.digital-time {
    font-size: clamp(25px, 7vw, 48px);
    font-weight: 800;
    letter-spacing: clamp(0px, .5vw, 3px);
    color: #45caff;
    text-shadow: 0 0 12px #008cff,
                 0 0 25px #008cff;
    white-space: nowrap;
}

/* Date */

.date {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 9px;
    margin-top: 15px;
    padding: 13px 8px;
    border: 1px solid #813bff;
    border-radius: 30px;
    background: #090e2b;
    color: #e3eaff;
    font-size: clamp(11px, 3vw, 14px);
    box-shadow: 0 0 15px #783dff35;
}

/* Button */

.toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-top: 24px;
    padding: 14px 25px;
    border: none;
    border-radius: 40px;
    background: linear-gradient(100deg,#087eff,#9c18ff);
    color: white;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: .3s ease;
    box-shadow: 0 0 20px #5545ff55;
}

.toggle:hover {
    transform: translateY(-3px);
    box-shadow: 0 0 30px #813cff;
}

.toggle:active {
    transform: scale(.96);
}

/* Tablet */

@media (max-width: 768px) {

    body {
        padding: 18px;
    }

    .clock-container {
        max-width: 430px;
    }

    .clock {
        width: min(300px, 100%);
    }
}

/* Mobile */

@media (max-width: 480px) {

    body {
        padding: 12px;
    }

    .clock-container {
        padding: 25px 16px;
        border-radius: 20px;
    }

    .clock-heading {
        margin-bottom: 25px;
        letter-spacing: 1px;
    }

    .clock {
        width: min(280px, 100%);
        border-width: 4px;
    }

    .digital-box {
        margin-top: 27px;
        padding: 13px 5px;
    }

    .date {
        padding: 12px 5px;
        gap: 6px;
    }

    .toggle {
        width: 100%;
        padding: 14px 10px;
    }
}

/* Small Mobile */

@media (max-width: 350px) {

    .clock-container {
        padding: 22px 12px;
    }

    .clock {
        width: 100%;
    }

    .digital-time {
        font-size: 25px;
    }

    .date {
        font-size: 10px;
    }

    .number {
        font-size: 17px;
    }
}

/* Reduced Motion */

@media (prefers-reduced-motion: reduce) {
    .clock::before {
        animation: none;
    }

    .toggle {
        transition: none;
    }
}
</style>
</head>

<body>

<div class="clock-container">

<div class="clock-content">

    <div class="clock-heading">
        ✦ JavaScript Animated Clock
    </div>

    <!-- Analog Clock -->

    <div class="clock" id="clock">

        <div id="ticks"></div>

        <div id="numbers"></div>

        <div class="hand hour" id="hour"></div>

        <div class="hand minute" id="minute"></div>

        <div class="hand second" id="second"></div>

        <div class="center"></div>

    </div>

    <!-- Digital Clock -->

    <div class="digital-box">

        <div class="digital-time" id="digital">
            00:00:00
        </div>

    </div>

    <!-- Date -->

    <div class="date">
        <span>▦</span>
        <span id="date">Loading date...</span>
    </div>

    <!-- Format Toggle -->

    <button class="toggle" id="toggle">
        <span>↻</span>
        <span id="toggleText">Switch 12 / 24 Hour</span>
    </button>

</div>
</div>

<script>

/* Elements */

const clock = document.getElementById("clock");
const ticks = document.getElementById("ticks");
const numbers = document.getElementById("numbers");

const hourHand = document.getElementById("hour");
const minuteHand = document.getElementById("minute");
const secondHand = document.getElementById("second");

const digital = document.getElementById("digital");
const dateDisplay = document.getElementById("date");

const toggle = document.getElementById("toggle");
const toggleText = document.getElementById("toggleText");

let format24 = false;


/* Generate Clock Markers */

function createTicks() {

    ticks.innerHTML = "";

    const size = clock.offsetWidth;
    const radius = size / 2 - size * 0.045;

    for (let i = 0; i < 60; i++) {

        const tick = document.createElement("div");

        tick.className = i % 5 === 0
            ? "tick major"
            : "tick";

        tick.style.transform =
            `translate(-50%, -50%) rotate(${i * 6}deg) translateY(-${radius}px)`;

        ticks.appendChild(tick);
    }
}


/* Generate Clock Numbers */

function createNumbers() {

    numbers.innerHTML = "";

    const size = clock.offsetWidth;
    const center = size / 2;
    const radius = size * 0.375;

    for (let i = 1; i <= 12; i++) {

        const number = document.createElement("div");

        number.className = "number";
        number.textContent = i;

        const angle = i * Math.PI / 6;

        const x = center + Math.sin(angle) * radius;
        const y = center - Math.cos(angle) * radius;

        number.style.left = x + "px";
        number.style.top = y + "px";

        numbers.appendChild(number);
    }
}


/* Create Clock */

function createClock() {
    createTicks();
    createNumbers();
}


/* Update Clock */

function updateClock() {

    const now = new Date();

    const hours = now.getHours();
    const minutes = now.getMinutes();
    const seconds = now.getSeconds();
    const milliseconds = now.getMilliseconds();


    /* Smooth Animation */

    const smoothSeconds =
        seconds + milliseconds / 1000;

    const smoothMinutes =
        minutes + smoothSeconds / 60;

    const smoothHours =
        (hours % 12) + smoothMinutes / 60;


    /* Analog Hands */

    secondHand.style.transform =
        `translateX(-50%) rotate(${smoothSeconds * 6}deg)`;

    minuteHand.style.transform =
        `translateX(-50%) rotate(${smoothMinutes * 6}deg)`;

    hourHand.style.transform =
        `translateX(-50%) rotate(${smoothHours * 30}deg)`;


    /* Digital Time */

    const displayHours = format24
        ? hours
        : (hours % 12 || 12);

    const suffix = hours >= 12 ? "PM" : "AM";

    const time =
        String(displayHours).padStart(2, "0") + ":" +
        String(minutes).padStart(2, "0") + ":" +
        String(seconds).padStart(2, "0");

    digital.textContent = format24
        ? time
        : time + " " + suffix;


    /* Live Date */

    dateDisplay.textContent =
        now.toLocaleDateString("en-IN", {
            weekday: "long",
            day: "2-digit",
            month: "long",
            year: "numeric"
        });
}


/* Toggle Format */

toggle.addEventListener("click", function() {

    format24 = !format24;

    toggleText.textContent = format24
        ? "Switch to 12 Hour"
        : "Switch to 24 Hour";

    updateClock();

});


/* Smooth Animation */

let animationFrame;

function animateClock() {

    updateClock();

    animationFrame = requestAnimationFrame(animateClock);

}


/* Initialize */

createClock();

animateClock();


/* Responsive Resize */

let resizeTimeout;

window.addEventListener("resize", function() {

    cancelAnimationFrame(resizeTimeout);

    resizeTimeout = requestAnimationFrame(createClock);

});

</script>

</body>
</html>

How to Customize the Clock

You can easily customize this clock project:

  1. Change the background color using the CSS body selector.
  2. Adjust the clock dimensions using the .clock class.
  3. Customize the analog clock hands using .hour, .minute, and .second.
  4. Change the digital clock color using .digital-time.
  5. Modify the glowing effects using CSS box-shadow.
  6. Customize the date format using JavaScript toLocaleDateString().

Conclusion

This Responsive JavaScript Animated Clock is a great front-end development project for learning HTML, CSS, and JavaScript. It combines a modern analog clock, digital time display, live date, smooth animations, and responsive design in one simple project.

You can use this animated clock on personal websites, dashboards, portfolios, or web development projects. Try customizing its colors, animations, and layout to create your own unique clock design.

If you found this tutorial helpful, explore more HTML, CSS, and JavaScript projects on MyCodingTutorial.

Post Rating
6.4 / 10
User rating: 6.4 / 10
Rate this post
Click a number to rate this article.
Mycodingtutorial

Web Designer & Developer

Hello, I’m Mohit, is a PHP Developer and Web Designer skilled in PHP, HTML, CSS, JavaScript, MySQL, and WordPress. He creates responsive, modern, and user-friendly websites with a focus on clean UI/UX design.

-->