JavaScript to display running time in the browser


Description


This script displays the running time in Hours, Minutes and Seconds format. The Date() function is used to fetch the current system date. getHours(), getMinutes and getSeconds() function is used to fetch hours, minutes and seconds respectively from the value of Date() function.

Code

<html>

<head>

<title>Display running time using JavaScript</title>

<script>

function starttime()

{

var today=new Date();

var hours=today.getHours();

var minutes=today.getMinutes();

var seconds=today.getSeconds();

var i;

var x;

x=checkampm(hours);

m=checktime(minutes);

s=checktime(seconds);

document.getElementById('txt').innerHTML=
"<font color=BLACK><h1>"+hours+":"+minutes+":"+seconds+" "+x+"</h1>";

t=setTimeout('starttime()',100);

}

function checktime(i)

{

if(i<10)

i="0"+i;

return(i);

}

function checkampm(i)

{

if(i<12)

return('AM');

else

return('PM');

}

</script>

</head>

<body onload=starttime();>

<span id='txt' style="background_color=WHITE">Time is</span>

</body>

</html>


Output


Display current time in the browser using JavaScript
Display time using JavaScript


No comments: