实现效果:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ac054d3f1d2ffec6664d7a05b1c08b6e.gif#pic_center)
代码实现:
<!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></div>
<script>
var div = document.querySelector('div');
getDate();
setInterval(getDate, 1000);
function getDate() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = month < 10 ? '0' + month : month;
var dates = date.getDate();
dates = dates < 10 ? '0' + dates : dates;
var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
var day = date.getDay();
var hour = date.getHours();
var half = hour >= 12 ? '下午' : '上午';
hour = hour <= 12 ? hour : (hour - 12);
hour = hour < 10 ? '0' + hour : hour;
var min = date.getMinutes();
min = min < 10 ? '0' + min : min;
var sed = date.getSeconds();
sed = sed < 10 ? '0' + sed : sed;
div.innerHTML = (year + '年' + month + '月' + dates + '日 ' + arr[day] + ' ' + half + hour + '点' + min + '分' + sed + '秒');
}
</script>
</body>
</html>