Display Time with JavaScript
In some cases, we have to show the system time dynamically.
In that case we need to consider current hours, minutes and seconds.
we can show browser time with the help of javascript dynamically.
For this I use the setTimeout function to display time dynamically.
The code to display time with javascript like below.
<html>
<head>
<script type="text/javascript">
function time_display(){
var time = new Date()
var hours = time.getHours()
var minutes = time.getMinutes()
minutes=((minutes < 10) ? "0" : "") + minutes
var seconds = time.getSeconds()
seconds=((seconds < 10) ? "0" : "") + seconds
var clock = hours + ":" + minutes + ":" + seconds
document.getElementById('lbl_time').innerHTML = clock
setTimeout("time_display()",1000)
}
</script>
</head>
<body onload="time_display();">
<form>
Time: <label id="lbl_time"></label>
</form>
</body>
</html>
When you open the file in Browser you can output like below.
Time:
I tested this with major browsers like firefox,IE ..., it works fine.
Download source code here
Subscribe
Filter by APML