ToTem's BLOG

日拱一卒,功不唐捐

用JAVASCRIPT语句实现电子时钟

实例化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>

阅读更多
个人分类: jscript
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭