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 🚀.
Before we start, here are some more JavaScript Games you might like to create:
1. Snake Game using JavaScript
2. 2D Bouncing Ball Game using JavaScript
3. Rock Paper Scissor Game using JavaScript
4. Tic Tac Toe Game using JavaScript
5. Whack a Mole Game using JavaScript
I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.
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
CSS CodeÂ
Create a file style.css
and paste the code below.
* { 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
JavaScript CodeÂ
script.js
 and paste the code below.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();