<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 id="date_time"></h1>
<script type="text/javascript">
function getWeek(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;//外国月从0开始
month = month < 10 ? "0" + month : month;
var date = dateTime.getDate();
date = date < 10 ? "0" + date : 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 = getWeek(day);
dateTime = year + "年" + month + "月" + date + "日" +" "+week+" "+hour + ":" + minute + ":" + second;
console.log(dateTime);//控制台输出
document.getElementById("date_time").innerHTML = dateTime;//body里输出
}
showTime();
// setInterval("showTime()");
setInterval("showTime()",1000);//间隔多少毫秒,执行一次方法,一般是一秒,默认也是
</script>
</body>
</html>
09-27
1398
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-08
1975
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)