Click here to see Digital Clock
<!DOCTYPE html>
<html>
<head>
<title>Digital Clock</title>
//Add following CSS
<style>
body {
font-family: sans-serif;
}
.clock {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 60px;
color: black;
}
</style>
<body>
//Add following code in Html
<div class="clock" id="clock"></div>
//Add following code of Javascript
<script>
function showTime() {
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
// Add a leading zero to the hours and minutes if necessary
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
// Display the time
var time = hours + ":" + minutes + ":" + seconds;
document.getElementById("clock").innerHTML = time;
}
// Update the time every second
setInterval(showTime, 1000);
</script>
</body>
</html>
Comments
Post a Comment