<span>当前时间:</span>
<span id="time"></span>
<span id="xq"></span>
<script type="text/javascript">
window.onload=function(){
//1s加载一次
setInterval("clock()",1000);
}
function clock () {
//全局变量d,创建Date对象
d = new Date();
//获得日期时间控件
var spt = document.getElementById("time");
//格式:2018/2/5 19:19:33
spt.innerHTML=d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日 "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
//获得星期控件
var spweek = document.getElementById("xq");
spweek.innerHTML=week();
}
function week () {
var weekday = new Array(7)
weekday[0] = "星期日"
weekday[1] = "星期一"
weekday[2] = "星期二"
weekday[3] = "星期三"
weekday[4] = "星期四"
weekday[5] = "星期五"
weekday[6] = "星期六"
return weekday[d.getDay()];
}
</script>
页面显示如下: