In this blog, we are going to make a digital clock that will give the current time and current date.
you can see a demo of this project.
pre-requisites:
- Basic HTML
- Basic CSS
- Basic JavaScript
for making this project you should have knowledge of these javascript concepts:
- DOM manipulation
- Date functions
- setTimeout
- if-else statement
make three files named:
- index.html
- style.css
- app.js
Html Code:(index.html)
<!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</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Digital clock</h1>
<div class="container">
<div id="time"></div>
<div id="clock"></div>
</div>
<script src="app.js"></script>
</body>
</html>
Css Code:(style.css)
body{ background-color: rgb(245, 17, 17); display: flex; flex-direction: column; justify-content: center; align-items: center; } h1{ font-size: 80px; text-decoration: underline solid rgb(170, 12, 243); text-align: center; } .container{ border: 10px solid black; border-radius: 10px; background-color: grey; box-shadow: 2px -2px 10px 5px blue; } #time{ font-size: 60px; padding:10px; color: white; } #clock{ color:white; font-size: 60px; letter-spacing: 10px; padding: 10px; text-align: center; } @media screen and (max-width:425px) { #time,#clock{ font-size: 30px; letter-spacing: 5px; text-align: left; } }
JavaScript Code:(app.js)
var time=new Date();
var x=time.toDateString();
document.getElementById("time").innerHTML=x;
function showdate(){
var date=new Date();
var hours=date.getHours();
var minutes=date.getMinutes();
var seconds=date.getSeconds();
var session="AM";
if(hours==0){
hours=12;
}
if(hours>12){
hours=hours-12;
session="PM";
}
if(hours<10){
hours="0"+hours;
}
if(minutes<10){
minutes="0"+minutes;
}
if(seconds<10){
seconds="0"+seconds;
}
document.getElementById("clock").innerHTML=hours+":"+minutes+":"+seconds+" "+session;
setTimeout(showdate,1000);
}
showdate();
0 Comments