<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>javascript时间</title>
</head>
<body>
<div id="div1"></div>
</body>
<script type="text/ecmascript">
function clock() {
var time = new Date();
var y = time.getFullYear();//年
var m = time.getMonth() + 1; //月 从0开始
var day = time.getDay();//日
var week = time.getDay();//星期几 从0开始,星期日是0
var h = time.getHours();//小时
var m = time.getMinutes();//分
var s = time.getSeconds();//秒
var ms = time.getMilliseconds();//毫秒
//alert(typeof day)
switch (week) {
case 0:
week = '星期日';
break;
case 1:
week = '星期一';
break;
case 2:
week = '星期二';
break;
case 3:
week = '星期三';
break;
case 4:
week = '星期四';
break;
case 5:
week = '星期五';
break;
case 6:
week = '星期六';
break
}
var str = '今天是' + y + '年' + m + '月' + day + '日' + week + h + '点' + m + '分' + s + '秒' + ms + '毫秒';
oDiv = document.getElementById('div1');
oDiv.style.fontSize = '24px';
oDiv.style.color = '#000';
oDiv.style.background = '#ccc';
oDiv.innerHTML = str
}
clock();
var timer = setInterval(clock, 1000);
//setInterval(clock, 1000);
oDiv.onclick = function () {
if (timer) {
clearInterval(timer)
timer = null;//停止的时候把timer清掉
} else {
clock();
timer = setInterval(clock, 1000)//运行的时候再把值赋给timer
}
}
</script>
</html>