User Tools

Site Tools


simple_javascript_clock

This is an old revision of the document!


<html>
<head>
<script type="text/javascript">
<!--

function pad(n,s) {
	n = n.toString();
	while (n.length < s) { n = "0" + n; }
	return n;
}

function updateClock ( )
{
	var now = new Date ( );

	var dows = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	var dow = now.getDay();

	var year = now.getFullYear();
	var mon = now.getMonth()+1;
	var day = now.getDate();

	var h = now.getHours ( );
	var m = now.getMinutes ( );
	var s = now.getSeconds ( );
	var ms = now.getMilliseconds();

	var ampm = 'a';

	if (h>=12) ampm = 'p';
	if (h>12) h-=12;
	else if (h==0) h=12;

	//var time_string = dows[dow] + " " + year + "-" + pad(mon,2) + "-" + pad(day,2) + " " + h + ":" + pad(m,2) + ":" + pad(s,2) + "." + pad(ms,3);
	var time_string = dows[dow] + " " + year + "-" + pad(mon,2) + "-" + pad(day,2) + " " + h + ":" + pad(m,2) + ampm;

	document.getElementById("clock").firstChild.nodeValue = time_string;

	// wait til the next minute + 500ms, that way we only update the clock on minute change
	// (for second-accuracy, use wait = 1000-ms)
	var wait = 60000 - (1000*s+ms) + 500;
	setTimeout('updateClock()', wait );
}

// -->
</script>
</head>
<body onload="updateClock();">
<span id=clock>&nbsp;</span>
</body>
</html>
simple_javascript_clock.1241545350.txt.gz · Last modified: 2009/05/05 10:42 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki