DEV Community

Cover image for Digital Clock using JavaScript
Piyush | Coding Torque
Piyush | Coding Torque

Posted on • Originally published at codingtorque.com

Digital Clock using JavaScript

Hello Guys! In this blog, I'm going to explain to you how to make a digital clock using javascript. You can use this project on your website to show the time to your users. I know users can see the time on their systems but you can use this project to show different time zones. This will be a step-by-step guide including HTML and CSS. Let's get started 🚀.

Let's cover HTML Part

We use HTML to make the skeleton of a website. HTML is a markup language.

Now let's import the font awesome CDN in our HTML <head> tag. fontawesome is a library that is used for icons in a website.

<!-- Font Awesome Icons  -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
Enter fullscreen mode Exit fullscreen mode

Now let's import the fonts using Google Fonts API. Below is the code for Poppins Font. Paste the below code in <head> tag.

<!-- Google Fonts  -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
Enter fullscreen mode Exit fullscreen mode

In the below HTML code, we have created a container that has a card div and inside the card div, we have a time display div with id 'clockDisplay' and onload attribute to invoke showTime() function on website loads.

<div class="container">
    <div class="card">
        <h1 id="day">Digital Clock</h1>
        <div id="clockDisplay" class="clock" onload="showTime()"></div>
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Here is the final HTML code

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Font Awesome Icons  -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
        integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
        crossorigin="anonymous" />

    <!-- Google Fonts  -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">

    <title>Digital Clock using JavaScript - @code.scientist x @codingtorque</title>
</head>

<body>
    <div class="container">
        <div class="card">
            <h1 id="day">Digital Clock</h1>
            <div id="clockDisplay" class="clock" onload="showTime()"></div>
        </div>
    </div>

</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Output Till Now

HTML Output of digital clock

Let's understand CSS part

In the below CSS code.

  1. We declare a * selectors for the font Poppins that we have imported in our head tag.
  2. Next, we declare a body selector which consists of styles for dark mode and aligns all elements in the body to the center.
  3. Next, we have a container and card with glassmorphism effect. I have created this Glassmorphic effect using Glass UI.
* {
    font-family: 'Poppins', sans-serif;
}

body {
    background-color: #111827;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
}

.container {
    border-radius: 20px;
    width: 30rem;
    background-color: #111927;
    background-image:
        radial-gradient(at 47% 33%, hsl(162.00, 77%, 40%) 0, transparent 59%),
        radial-gradient(at 82% 65%, hsl(218.00, 39%, 11%) 0, transparent 55%);
}

.card {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 30px 80px;
    backdrop-filter: blur(16px) saturate(180%);
    -webkit-backdrop-filter: blur(16px) saturate(180%);
    background-color: rgba(17, 25, 40, 0.75);
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.125);
}


.clock {
    font-size: 30px;
    font-weight: bold;
}   
Enter fullscreen mode Exit fullscreen mode

Output Till Now

CSS Output of digital clock

Finally a JavaScript part

function showTime() {
    let date = new Date();
    let h = date.getHours(); // 0 - 23
    let m = date.getMinutes(); // 0 - 59
    let s = date.getSeconds(); // 0 - 59
    let session = "AM";
    let day = date.getDay();

    if (h == 0) {
        h = 12;
    }

    if (h > 12) {
        h = h - 12;
        session = "PM";
    }

    h = (h < 10) ? "0" + h : h;
    m = (m < 10) ? "0" + m : m;
    s = (s < 10) ? "0" + s : s;

    let time = h + " : " + m + " : " + s + " " + session;
    document.getElementById("clockDisplay").innerText = time;
    document.getElementById("clockDisplay").textContent = time;

    setTimeout(showTime, 1000);

}
showTime();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)