JavaScript Tutorial/Animation/Clock
Clock
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function gettime() {
var date= new Date();
var hr = date.getHours();
var m = date.getMinutes();
var s = date.getSeconds();
var ampm="AM";
if (hr > 11)
{
ampm="PM"
}
if (hr > 12)
{
hr -= 12
}
if(m < 10)
{
m = "0" + m
}
if(s < 10)
{
s = "0" + s
}
document.clockform.clock.value = hr + ":" + m + ":" + s + " " +ampm;
setTimeout("gettime()",100)
}
// -->
</script>
</head>
<body onload="gettime()">
<form name="clockform">
<input type="text" name="clock">
</form>
</body>
</html>
Status bar clock (IE)
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
function theclock() {
var rightnow= new Date();
var themonth= rightnow.getMonth();
var today= rightnow.getDate();
var theyear= rightnow.getYear();
var thehours= rightnow.getHours();
var themins= rightnow.getMinutes();
var theseconds= rightnow.getSeconds();
// month starts at 0
themonth+=1;
if (theyear<2000)
theyear+=1900;
if (thehours<10)
thehours="0"+thehours;
if (themins<10)
themins="0"+themins;
if (theseconds<10)
theseconds="0"+theseconds;
if (thehours<12)
var ampm="AM";
else
var ampm="PM";
if (thehours==0)
thehours=12;
if (thehours>=13)
thehours-=12;
window.status= themonth+"/"+today+"/"+theyear+" "+thehours+":"+themins+":"+theseconds+" "+ampm;
}
setInterval("theclock()",1000);
//-->
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>