Cool Digital Clock

Syiainfoku make a Cool Digital Clock

Cool Digital Clock

Hello everyone, I am the founder of Syiainfoku. Today in Syiainfoku you will learn how to create a Digital Clock With Vanilla JavaScript. In the previous post we learned how to make an analog clock, so today we are going to make a digital version.

Digital Clock is also a JavaScript Mini Project, and very useful to train you, if you are just starting to learn JS. And many people suggest you to practice creating some Mini JS Projects so you understand JS better.

Preview Cool Digital Clock

Preview Cool Digital Clock

Source Code and Brief Explanation

To make this program [Digital Clock with Vanilla JavaScript]. First you need to create three Files, one HTML File and one CSS File and lastly one JS File. After creating these files, just paste the following code into your file. You can also download the source code file of this Digital Clock from the given download button.

First, create an HTML file with the name index.html and paste the given code in your HTML file. Remember, you must create a file with an .html extension. 

<!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 with Dark UI | hitoricoding</title>
<link rel="stylesheet" href="./assets/style.css">
</head>

<body>

<div class="clock">
<div class="display"></div>
</div>

<script src="./assets/main.js"></script>
</body>

</html>

Second, create a CSS file with the name style.css and paste the given code in your CSS file. Remember, you must create a file with a .css extension.

@import url('https://fonts.googleapis.com/css2?family=Orbitron&display=swap');

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

html,
body {
height: 100%;
}

body {
display: grid;
place-items: center;
background-color: #050711;
}

.clock {
background-color: #050711;
height: 120px;
width: 550px;
line-height: 120px;
text-align: center;
padding: 0 5px;
box-shadow: -3px -3px 7px rgba(255, 255, 255, 0.05),
3px 3px 5px rgba(0, 0, 0, 0.5);
}

.clock .display {
font-size: 60px;
color: #1bebe0;
letter-spacing: 5px;
font-family: 'Orbitron', sans-serif;
}

Finally, create a JS file with the name main.js and paste the given code in your JS file. Remember, you must create a file with a .js extension.

setInterval(function () {
const clock = document.querySelector(".display");
let time = new Date();
let sec = time.getSeconds();
let min = time.getMinutes();
let hr = time.getHours();
let day = 'AM'
if (hr > 12) {
day = 'PM';
hr = hr - 12;
}
if (hr == 0) {
hr = 12;
}
if (sec < 10) {
sec = '0' + sec;
}
if (min < 10) {
min = '0' + min;
}
if (hr < 10) {
hr = '0' + hr;
}
clock.textContent = hr + ':' + min + ':' + sec + " " + day;
});

That's it, now you have successfully created a Digital Clock with Vanilla JavaScript, If your code is not working or you are facing any error/problem, please download the source code file from the given download button. It's free and a .zip file will be downloaded then you have to extract it. 

Closing

Thank you for those of you who have read and downloaded this source code, hopefully it can be useful and add to your insight.

If you found this article useful, you can share it. That's all from me, and THANK YOU