How to make Digital Clock JavaScript
Published: 2023-09-05 05:39:15

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.

Here Source Code  [link ](https://stakedesigner.com/)

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.

## Prerequisites for Digital Clock JavaScript

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

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


## 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.

## HTML
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">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Digital Clock</title>
    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@600;800&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="clock">
      <div id="hour"></div>
      <span>:</span>
      <div id="minute"></div>
      <span>:</span>
      <div id="seconds"></div>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>`

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.

## CSS:
`* {
  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;
}`

## Javascript:

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);`



## Support 🤗
[YouTube](https://www.youtube.com/@stakedesigner)
[Website](https://stakedesigner.com/)

## Thanks for reading!

If you have any questions, complaints or tips, you can leave them here in the comments. I will be happy to answer!

😊😊 See you later! 😊😊