Search This Blog

Monday 30 April 2012

How to display current time using javscript?


How ro create a Javascript clock?
Most of the websites are shown current time on top or bottom. But if we want to display updated time that means updated by each seconds, we can implemented JavaScript timer for the same. In some of the case we need to display count time or stopwatch have to display on our website, that time also we can implement same technique.

Javascript code for dynamic timer.
Below are the javascript codes for timer. There is a default function setTimeout available in javscript for repeated execution for a particular period.

<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('timeDiv').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',00);
}

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

<body onload="startTime()" bgcolor="white">
<div id="timeDiv"></div>
</body>
</html> 

No comments:

Post a Comment