Monday, October 04, 2010

JavaScript: Display Clock

Displaying a clock is very similar to making a countdown timer. All we have to do is to create a new date and get it's hours, minutes, and seconds.

Here is a example:

<!-- This span is where the clock will appear -->
<div id='clockDiv'></div>
<script type="text/javascript">
function clock() {
   var now = new Date();
   var outStr = now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();
   document.getElementById('clockDiv').innerHTML=outStr;
   setTimeout('clock()',1000);
}
clock();
</script>

Hope this helps :-)

No comments:

Post a Comment