Home > DotNet, Javascript > How to display current time using javascript?

How to display current time using javascript?


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="blue">
<div id="timeDiv"></div>
</body>
</html>

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: