DEV Community

loading...
Cover image for Build your own digital clock using JavaScript

Build your own digital clock using JavaScript

abdurrehmaan profile image Abdur Rehman Khalid ・3 min read

Source code : Github

1212

In this article, I am going to show you how to design a simple animated Digital Clock in JavaScript. Since, a browser executes a JavaScript program at the client side, the script will pick up time from the client's computer and display it.

What we want to have

  • Display current date
  • Display current time
  • Increment the time on it's own

Technology we use

  • Vanilla javascript

The Javascript

The entire code for the working of the clock is written within the tick() function. Inside this function, an object of the Date() is created which allows you to call year, date, hour, minute, second.

const now = new Date();

In our code, this object is used for getting the current hours, minutes and seconds which are stored in different variables.

const h = now.getHours();
const m = now.getMinutes();
const s = now.getSeconds();

The obtained hours, minutes and seconds will be displayed in single digit if less than 10. For example, the current hour will be displayed as 7 instead of 07. To always display the elements of time in two-digit format, a 0 is appended before them whenever they are less than 10

<span>${h < 10 ? "0"+h:h}</span>:
<span>${m < 10 ? "0"+m:m}</span>:
<span>${s < 10 ? "0"+s:s}</span>`;

Now once our time is ready, let's display it in the div which we made before. This is done by obtaining the div using the document.getElementById method and give our time as the content of the div using the innerHTML property.

const clock = document.querySelector('.clock');
const html = `
<span>${h < 10 ? "0"+h:h}</span>:
<span>${m < 10 ? "0"+m:m}</span>:
<span>${s < 10 ? "0"+s:s}</span>`;
clock.innerHTML = html;

Base design

Make the basic html structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Digital Clock</title>
</head>
<body>
    <div class="clock-body">
        <div class="inner-body">
            <div class="clock">
            
            </div>
        </div>
    </div>
</body>
</html>

Attach CSS

<link rel="stylesheet" href="css/digitalclock.css">

Attach Javascript file

<script src="js/digitalclock.js"></script>

The Styling

Will add a nice font it will center the hero div and make the background fit nicely

@import url('https://fonts.googleapis.com/css?family=Orbitron');
.clock-body {
    margin: 200px auto;
    height: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
}
.inner-body{
    width: 30%;
    height: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 5px solid #2BC511;
    background-color: black;
    border-radius:12px;
}
.clock{
    font-size: 4em;
    font-weight: 700;
    text-align: center;
    color:#2BC511;
    font-family: 'Orbitron', sans-serif;
}
.clock span {
    padding: 20px;
}

Discussion (3)

Collapse
brandonwallace profile image
brandon_wallace

I suggest using a monospace font for the digits because you will get text shifting every time the number changes to 1 due to the fact that number is more narrow than the others. By the way, nice project.
You can add some responsiveness with a media query so that it fits on the screen of a phone.

Collapse
fahadhassan1213 profile image
Fahad Hassan

I think its a good project for beginner to add on their projects showcase
clear and well explained!

Collapse
afif profile image
Temani Afif

You can have your code formatted with coloration if you add the language like this: dev-to-uploads.s3.amazonaws.com/up...

Forem Open with the Forem app