User Tools

Site Tools


simple_javascript_clock

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

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

function getTimeFormatted() {
	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;

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

}

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

	var s = now.getSeconds ( );
	var ms = now.getMilliseconds();

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

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


// -->
</script>
</head>
<body>
	<span style="" id=clock>&nbsp;</span>
	<script>
		updateClock();
		document.getElementById("clock").title = "Page loaded: " + getTimeFormatted();
	</script>
</body>
</html>


simple_javascript_clock.txt · Last modified: 2009/05/05 10:59 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki