Sitemap
Chamod Dilshan 😎

Chamod Dilshan 😎

I'm Chamod Dilshan! πŸš€ Entrepreneur πŸš€ ICT undergrad, web dev, and tech enthusiast. Wordsmith on the side. Let's explore tech together

Digital Clock in JavaScript | Source Code

1 min readJun 29, 2024

--

Press enter or click to view image in full size

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My website</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>

<div id="clockcontainer">
<div id="clock">00:00:00</div>
</div>








<script src="script.js"></script>
</body>
</html>

Get Chamod Dilshan πŸ˜Žβ€™s stories inΒ yourΒ inbox

Join Medium for free to get updates fromΒ thisΒ writer.

CSS


body{
margin: 0;
/* background: rgb(131,58,180);
background: linear-gradient(90deg, rgba(131,58,180,1) 0%, rgba(253,29,29,1) 50%, rgba(252,176,69,1) 100%); */
background-image: url(https://img.freepik.com/free-vector/dark-background-gradient-geometric-shapes_23-2148430297.jpg?size=626&ext=jpg&ga=GA1.1.1482922746.1719308886&semt=ais_user);
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
background-position: center;
}
#clockcontainer{
height: 70vh;
width: 100vh;
margin: auto;
display: flex;
justify-content: center;
align-items: center;
margin-top: 100px;

/* From https://css.glass */
background: rgba(255, 255, 255, 0.196);
border-radius: 16px;
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
backdrop-filter: blur(12.8px);
-webkit-backdrop-filter: blur(12.8px);
border: 1px solid rgba(255, 255, 255, 0.3);
}

#clock{
font-family: sans-serif;
font-size: 5.5rem;
font-weight: bold;
text-align: center;
color: white;
}

JavaScript


function updateClock(){

const now = new Date();

let hours = now.getHours();

const meridiem = hours > 12 ? "PM" : "AM";

hours = hours % 12 || 12;

hours = hours.toString().padStart(2,0)

const minutes = now.getMinutes().toString().padStart(2,0);
const seconds = now.getSeconds().toString().padStart(2,0);

const timeString = `${hours}:${minutes}:${seconds} ${meridiem}`;

document.getElementById("clock").textContent = timeString;
}

updateClock();

setInterval(updateClock,1000);

Thank you. Follow Fore more. Check Out my Portfolio

--

--

Chamod Dilshan 😎
Chamod Dilshan 😎

Written by Chamod Dilshan 😎

I'm Chamod Dilshan! πŸš€ Entrepreneur πŸš€ ICT undergrad, web dev, and tech enthusiast. Wordsmith on the side. Let's explore tech together