代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="box"></div>
<script>
var box = document.getElementById('box');
var weeks = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
setInterval(auto, 1000);
function auto() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var weekDay = date.getDay();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
box.innerHTML = year + '年' + month + '月' + day + '日,' + weeks[weekDay] + ' ' + addZero(hour) + ':' + addZero(minute) + ':' + addZero(second);
}
auto();
// 加0封装
function addZero(time) {
// if (time >= 10) {
// return time = time;
// } else {
// return time = '0' + time
// }
return time = time >=10? time:'0'+time
}
</script>
</body>
</html>
效果