Digital Clock using JavaScript
Published: 2022-09-11 08:41:36

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.

```html
<!-- 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.

```html
<!-- 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.

```html
<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**

```html
<!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 of digital clock](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9buc2e87qwfiug3fpdhi.png)

## **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](https://ui.glass/generator/).

```css
* {
    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 of digital clock](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c2o3nvhvmhb7u5bgwt9t.png)

## **Finally a JavaScript part**


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