Building A Simple Analog Clock Using HTML, CSS, and JavaScript

Analog clocks are timeless pieces of art and functionality that continue to intrigue and captivate us. In this blog post, we will walk through the process of creating a simple analog clock using HTML, CSS, and JavaScript. This project will not only give you a better understanding of web development but also provide you with a functional and visually appealing clock.

HTML Structure

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <div class="clock">
    <div class="hands">

      <div class="hand second"></div>
      <div class="hand min"></div>
      <div class="hand hour"></div>
  <script src="script.js"></script>


    box-sizing: border-box;
    margin: 0;
    padding: 0;
    justify-content: center;
    align-items: center;
    height: 100vh;
    width: 30vw;
    height: 30vw;
    border: 3.5vw dotted black;
    border-radius: 50%;
    position: relative;
    width: 11vw;
    position: absolute;
    top: 49.6%;
    left: 1%;
    transform-origin: right;
    height: 0.2vw;
    background-color: black;

    width: 8vw;
    left: 14.1%;
    width: 6vw;
    left: 23%;



    let date= new Date();
    let hour= date.getHours();
    let minutes=date.getMinutes();
    let seconds=date.getSeconds();

 1. `setInterval` Function`: This function is used to repeatedly execute a given function (in this case, an anonymous function) at a specified time interval. In your example, the function is set to run every 1000 milliseconds (1 second).

2. `new Date()`: This creates a new JavaScript `Date` object, which holds the current date and time.

3. `getHours()`, `getMinutes()`, `getSeconds()`: These are methods of the `Date` object that extract the current hour, minute, and second, respectively.

4. DOM Manipulation: The code uses `document.querySelector()` to select elements with specific class names (`.second`, `.min`, `.hour`) in the HTML document. It then modifies the `style.transform` property of these elements to rotate them, simulating the movement of clock hands.

5. Rotation Calculation: The `rotate` property in the `style.transform` is set using CSS's `rotate` transformation. The degrees for rotation are calculated based on the current time.

   - For the seconds hand, it rotates `(seconds/60) * 360` degrees to represent the seconds, plus 90 degrees to initially position it.
   - For the minutes hand, it rotates `(minutes/60) * 360` degrees, again adding 90 degrees for initial positioning.
   - For the hour hand, it rotates `(hour/12) * 360` degrees plus an additional `(minutes/60) * 30` degrees (since each hour is divided into 60 minutes).

This code, when executed in a web browser, continuously updates the clock hands' positions to reflect the current time, creating the appearance of a functioning analog clock. Get the source code from GitHub.

Trending Posts

How To Create A Magic 8 Ball Using HTML, CSS, and JavaScript?

8 Characteristics Of Modern Computer

How To Create a Fake Facebook Login Clone Using Only HTML and CSS?

Supercharge Your Business: Digital Innovation Tactics

Introduction To Web Development?

Sunrise And Sunset Animation Using HTML & CSS?

How Long Does It Take To Become A Front-end Web Developer?

How To Create a Fan Using Only HTML and CSS?

How To Earn Money With Only HTML and CSS Knowledge?

Creating a Netflix-inspired Animation using HTML and CSS