How To Create A Digital Clock With Javascript

This tutorials aims to explain how to create a digital clock with javascript. If we are creating a clock then we need to get the current day and time. To get the current date we need to enter the follwing code :

var today=new Date();

After getting the current date now we need to get the current time. To get the current time we need to enter the following code :

var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();

Complete code to create a clock with javascript

<html>
<head>
<script type=”text/javascript”>
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById(‘txt’).innerHTML=h+”:”+m+”:”+s;
t=setTimeout(‘startTime()’,500);
}

function checkTime(i)
{
if (i<10)
{
i=”0″ + i;
}
return i;
}
</script>
</head>

<body onload=”startTime()”>
<div id=”txt”></div>
</body>
</html>

Loveish Kalsi

Loveish Kalsi is a freelance Web and Graphic Designer from India. He is the owner of famous design blog Creativedesignmagazine.com

Yazara ait tüm yazılar →

Related posts:

Leave a reply