How To Display Current Time on Page using Javascript

How To Display Current Time on Page using Javascript

In this tutorial we will learn How To Display Current Time on Page using Javascript. We are using JavaScript to accomplish this.

Code:

<html>
<head>
<title>How To Display Current Time on Page using Javascript</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
 }
</style>
<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('time').innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);
}

function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}
</script>
</head>
<h2>How To Display Current Time on Page using Javascript</h2>
<body onload="startTime()">
<div id="time"></div>
<br /><br />
<br /><br />
All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a> | Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>
</body>
</html>