Search This Blog

Loading...

Nov 10, 2010

How to display current time in JavaScript


Sponsored Links:
function PrintCurrentTime() 
{

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}

if (minutes < 10)
minutes = "0" + minutes

document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>")
}

Add JS reference in your aspx page as..
<script language="javascript" src="JScript.js"></script>

then make a call to PrintTime function as

<script type="text/javascript">
<!--
PrintCurrentTime();
//-->

</script>


Output:


image

Follow by Email