How to make Digital Clock JavaScript

Digital Clock JavaScript

Welcome to yet another exciting tutorial by stakedesigner. Building Digital Clock JavaScript projects is one of the best ways to learn javascript. So today let’s build a Digital Clock JavaScript’. To build this project we need HTML, CSS, Javascript.

In today’s digital age, knowing how to create a digital clock using JavaScript is a valuable skill for web developers. Whether you want to display the current time on your website or build a custom clock application, JavaScript provides the necessary tools to make it happen. In this tutorial, we’ll walk you through the process of creating a simple digital clock using HTML, CSS, and JavaScript.

Video Tutorial for Digital Clock JavaScript:

Welcome to yet another exciting tutorial by Digital Clock JavaScript. Building projects is one of the best ways to learn JavaScript. So today let’s build a ‘Digital Clock’. To build this project we need HTML, CSS, JavaScript.

Prerequisites for Digital Clock JavaScript

Before we dive into the code, make sure you have the following:

  1. Basic knowledge of HTML, CSS, and JavaScript.
  2. A code editor (e.g., Visual Studio Code, Sublime Text).
  3. A web browser to test your digital clock.
Digital Clock JavaScript
Digital Clock JavaScript

Project Folder Structure:

Let us explore the project folder structure. The project folder consists of 3 files. The HTML file creates the elements required to build the structure and layout of our project. Next, the CSS file styles the elements that we have created with CSS and Finally, Javascript adds functionality to our project.


We begin with the HTML code. Copy the code below and paste it into your HTML document. Here, we create a div with the class ‘clock’ and inside this, we create divs for displaying hours, minutes, and seconds. Apart from this we also use span tags to display ‘:’ between the divs.

<!DOCTYPE html>
<html lang="en">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Digital Clock</title>
    <!-- Google Fonts -->
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
    <div class="clock">
      <div id="hour"></div>
      <div id="minute"></div>
      <div id="seconds"></div>
    <!-- Script -->
    <script src="script.js"></script>


Next, we style our game using CSS. For this copy, the code provided to you below and paste it into your stylesheet.

We provide a background color to the body, while the time is displayed on a white background. Each element is aligned correctly and given a bit of a curve to make it look good.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
body {
  height: 100vh;
  background-image: linear-gradient(135deg, #8052ec, #d161ff);
.clock {
  width: 31.25em;
  height: 8em;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
.clock div {
  position: relative;
  background-color: #ffffff;
  height: 100%;
  width: 3.2em;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5em;
  color: #150c41;
  border-radius: 0.4em;
  box-shadow: 0 1em 2em rgba(0, 0, 0, 0.3);
  letter-spacing: 0.05em;
.clock span {
  font-weight: 800;
  font-size: 2.5em;
  color: #ffffff;


Finally, we add functionality using Javascript. Once again copy the code below and paste it into your script file.

We create a ‘clock’ variable in which we call the function ‘time’, this function updates the time every second by getting the current time using the JavaScript Date() object. The hours, minutes, and seconds are then extracted from the date and formatted with leading zeros using the padStart() function. Next, formatted time is then displayed on the webpage as “hour”, “minute”, and “seconds”.

const hour = document.getElementById("hour");
const minute = document.getElementById("minute");
const seconds = document.getElementById("seconds");

const clock = setInterval(function time() {
  const dateNow = new Date();
  let hr = dateNow.getHours();
  let min = dateNow.getMinutes();
  let sec = dateNow.getSeconds();

  hr = hr.toString().padStart(2, "0");
  min = min.toString().padStart(2, "0");
  sec = sec.toString().padStart(2, "0");

  const timeString = `${hr}:${min}:${sec}`;
  hour.textContent = hr;
  minute.textContent = min;
  seconds.textContent = sec;
}, 1000);

Go ahead and customize the project the way you like. If you have any queries, suggestions or feedback comment them below. Download the source code by clicking on the ‘ Download Code’ button below.

One thought on “How to make Digital Clock JavaScript

Leave a Reply