Thursday, November 8, 2012

Display Current Time using Javascript

  Today, we learn about how to display current time using JavaScript language.Here we can use javascript
Date object.using this object we can get hours,minute,second by getHours(),getMinutes() and getSeconds().
By using this method we can get the current system or server time.In below Example we provide all script.just run this file.






 Lets see the Below Code for :

<!DOCTYPE html>
<html>
<head>
<script>
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(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>


Output :
05:30:25


No comments:

Post a Comment

Thanks....