效果如下:
1.代码如下:
<h1 id="date_time"></h1>
<script type="text/javascript">
function Week(option) {
// switch(option){
// case 0:
// return "星期日";
// case 1:
// return "星期一";
// case 2:
// return "星期二";
// case 3:
// return "星期三";
// case 4:
// return "星期四";
// case 5:
// return "星期五";
// case 6:
// return "星期六";
// }
var week = "日一二三四五六".charAt(option);
return "星期" + week;
}
function showTime() {
var dateTime = new Date();
var year = dateTime.getFullYear();//年
var month = dateTime.getMonth() + 1;//月
month = month < 10 ? "0" + month : month;
var month1 = month;
var date = dateTime.getDate();//日
date = date < 10 ? "0" + date : date;
var date1 = date;
var hour = dateTime.getHours();//时
hour = hour < 10 ? "0" + hour : hour;
var minute = dateTime.getMinutes();//分
minute = minute < 10 ? "0" + minute : minute;
var second = dateTime.getSeconds();//秒
second = second < 10 ? "0" + second : second;
var day = dateTime.getDay();
var week = Week(day);//星期
dateTime = year + "年" + month1 + "月" + date1 + "日" + " " + week + " " + hour + ":" + minute + ":" + second;
document.getElementById("date_time").innerHTML = dateTime;
}
showTime();
setInterval("showTime()", 1000);
</script>