实例化date类
today = new Date();
分别取时、分、秒的数值
intHours = today.getHours();
intMinutes = today.getMinutes();
intSeconds = today.getSeconds();
格式化待输出的样式
if (intHours == 0) {
hours = "12:";
xfile = "午夜 ";
} else if (intHours < 12) {
hours = intHours+":";
xfile = "上午 ";
} else if (intHours == 12) {
hours = "12:";
xfile = "正午 ";
} else {
intHours = intHours - 12
hours = intHours + ":";
xfile = "下午 ";
}
if (intMinutes < 10) {
minutes = "0"+intMinutes+":";
} else {
minutes = intMinutes+":";
}
if (intSeconds < 10) {
seconds = "0"+intSeconds+" ";
} else {
seconds = intSeconds+" ";
}
timeString = xfile+hours+minutes+seconds;
定位到文档中输出
document.all.Clock.innerHTML = timeString;
显示间隔
window.setTimeout("tick();", 100);
装载:
<body onLoad="tick()">
设此DIV的ID值为CLOCK,则在JAVASCRIT定义的输出即会在此显示
<div id="Clock" style="position:relative;top:-2px;"></div>
</body>