实现的效果:
ps:代码中getDate()表示获取当前是几号,getDay()表示获取当前是星期几,getDay() 方法可返回一周(0~6)的某一天的数字。 星期天为 0, 星期一为 1, 以此类推。
<script type="text/javascript">
window.onload = function(){
var oDiv1 = document.getElementById('div1');
function getTime(){
var now = new Date();
var year = now.getFullYear();
var mon = now.getMonth()+1;
var day = now.getDate();
var week = now.getDay();
var hour = now.getHours();
var min = now.getMinutes();
var sec = now.getSeconds();
oDiv1.innerHTML = "北京时间现在是"+year+"年"+todo(mon)+"月"+todo(day)+"日"+
toweek(week)+todo(hour)+":"+todo(min)+":"+todo(sec)
}
setInterval(getTime,1000)
function todo(num){
if(num<10){
return "0"+num;
}else{
return num;
}
}
function toweek(num){
switch(num){
case 0:
return '星期天';
break;
case 1:
return '星期一';
break;
case 2:
return '星期二';
break;
case 3:
return '星期三';
break;
case 4:
return '星期四';
break;
case 5:
return '星期五';
break;
case 6:
return '星期六';
break;
}
}
}
</script>
</head>
<body>
<div id='div1'></div>
</body>