The Wayback Machine - https://web.archive.org/web/20250101010938/https://www.geeksforgeeks.org/how-to-create-analog-clock-using-html-css-and-javascript/
Open In App

Create an Analog Clock using HTML, CSS and JavaScript

Last Updated : 17 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

An analog clock displays time using a traditional clock face with hour, minute, and second hands. Creating an analog clock using HTML, CSS, and JavaScript involves designing the clock face with HTML and CSS, and using JavaScript to dynamically rotate the hands according to the current time.

Prerequisite

Approach

  • HTML: It is a simple file having the basic structure of the webpage and ID for the clock’s body and for the second, minute, and hour hands.
  • CSS: The CSS is used just for making the clock actually look a bit nicer. We have basically centered our clock in the middle of the webpage.
  • JavaScript: The JavaScript file will provide the logic behind the rotation of the hands.
    • First, we have selected the hour, minute, and second from HTML.
    • To get the current time we have used the Date() object provided by the JavaScript. This will give the current seconds, minutes, and hours respectively.
    • Now, we have got our hour, minute, and second, and we know that the clock rotates 360 degrees. So, we will convert to convert the rotation of the hands of the clock into degrees. The degree calculation is based on a simple unary method.

Example: This example shows the implementation of the above-explained approach.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Analog Clock</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <div class="clock">
        <div class="clock-face">
            <div class="number number1">1</div>
            <div class="number number2">2</div>
            <div class="number number3">3</div>
            <div class="number number4">4</div>
            <div class="number number5">5</div>
            <div class="number number6">6</div>
            <div class="number number7">7</div>
            <div class="number number8">8</div>
            <div class="number number9">9</div>
            <div class="number number10">10</div>
            <div class="number number11">11</div>
            <div class="number number12">12</div>
            <div class="hand hour-hand"></div>
            <div class="hand minute-hand"></div>
            <div class="hand second-hand"></div>
        </div>
    </div>
    <script src="script.js"></script>
</body>

</html>
CSS
/* styles.css */
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background: #f0f0f0;
    margin: 0;
}

.clock {
    width: 300px;
    height: 300px;
    border: 20px solid black;
    border-radius: 50%;
    position: relative;
    padding: 20px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
    background: white;
}

.clock-face {
    position: relative;
    width: 100%;
    height: 100%;
    transform: translateY(-3px); /* Fix for the hands positioning */
}

.hand {
    width: 50%;
    background: black;
    position: absolute;
    top: 50%;
    transform-origin: 100%;
    transform: rotate(90deg);
    transition: all 0.05s;
    transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
}

.hour-hand {
    height: 6px;
}

.minute-hand {
    height: 4px;
}

.second-hand {
    height: 2px;
    background: red;
}

.number {
    position: absolute;
    font-size: 24px;
    transform: translate(-50%, -50%);
}

.number1  { top: 20%; left: 85%; }
.number2  { top: 35%; left: 92%; }
.number3  { top: 50%; left: 97%; }
.number4  { top: 65%; left: 92%; }
.number5  { top: 80%; left: 85%; }
.number6  { top: 85%; left: 50%; }
.number7  { top: 80%; left: 15%; }
.number8  { top: 65%; left: 8%; }
.number9  { top: 50%; left: 3%; }
.number10 { top: 35%; left: 8%; }
.number11 { top: 20%; left: 15%; }
.number12 { top: 15%; left: 50%; }
JavaScript
// script.js
const hourHand = document.querySelector('.hour-hand');
const minuteHand = document.querySelector('.minute-hand');
const secondHand = document.querySelector('.second-hand');

function setDate() {
    const now = new Date();

    const seconds = now.getSeconds();
    const secondsDegrees = ((seconds / 60) * 360) + 90;
    secondHand.style.transform = `rotate(${secondsDegrees}deg)`;

    const minutes = now.getMinutes();
    const minutesDegrees = ((minutes / 60) * 360) + ((seconds / 60) * 6) + 90;
    minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;

    const hours = now.getHours();
    const hoursDegrees = ((hours / 12) * 360) + ((minutes / 60) * 30) + 90;
    hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
}

setInterval(setDate, 1000);

setDate();

Output:

im3

Next Article

Similar Reads

three90RightbarBannerImg