Digital Clock with HTML, CSS & JavaScript
01/30/2021
Demo
Video
Code
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Digital Clock with HTML, CSS & JavaScript</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="clock">
<div class="clock-date">Sunday, January 01, 2022</div>
<div class="clock-time">00:00:00</div>
</div>
<script src="clock.js"></script>
</body>
</html>
CSS
@charset "utf-8";
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap');
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 16px;
}
body {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
background-color: #000;
font-family: 'Roboto', sans-serif;
}
.clock {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
position: relative;
z-index: 10;
padding: 30px 30px 20px;
border-radius: 10px;
background-color: #3551ff;
color: #fff;
letter-spacing: 1px;
}
.clock::before {
position: absolute;
top: -2px;
right: -2px;
bottom: -2px;
left: -2px;
z-index: -1;
filter: blur(10px);
border-radius: 10px;
background-color: #3551ff;
content: '';
}
.clock-date {
font-size: 1.25rem;
}
.clock-time {
font-size: 5rem;
}
@media screen and (max-width: 480px) {
.clock {
padding: 20px 20px 10px;
}
.clock-date {
font-size: .9375rem;
}
.clock-time {
font-size: 3.75rem;
}
}
JavaScript
function clock() {
let d = new Date();
let year = d.getFullYear();
let month = d.toLocaleString("en-US", {
month: "long",
});
let date = ("0" + d.getDate()).slice(-2);
let dayNum = d.getDay();
let day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][dayNum];
let hr = ("0" + d.getHours()).slice(-2);
let min = ("0" + d.getMinutes()).slice(-2);
let sec = ("0" + d.getSeconds()).slice(-2);
document.querySelector(".clock-date").innerHTML = day + ", " + month + " " + date + ", " + year;
document.querySelector(".clock-time").innerHTML = hr + ":" + min + ":" + sec;
}
setInterval(clock, 1000);