Digital Clock using HTML, CSS & JavaScript | Coding Torque


 

Digital Clock using JavaScript - Coding Torque

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 the HTML part

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

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" />


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">


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.


Next we have calculate button which have onclick attribute with calculateLoan() function

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


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>


Output Till Now

HTML Output - Digital clock


Let's understand CSS Part

In the below CSS code,

  1. We declare a * selectors to set 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;
}


Output Till Now

CSS Output - 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();


Final Output





If you want me to code any project or post any specific post, feel free to DM me at IG @code.scientist or @codingtorque

 If you have any doubt or any project ideas feel free to Contact Us 

Hope you find this post helpful💖 

 Written by: Coding Torque | Piyush Patil 

 Follow us on Instagram for more projects like this👨‍💻 

Post a Comment

Previous Post Next Post