<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div,p{
width: 300px;
height: 30px;
line-height: 30px;
color:black;
background-color: pink;
}
</style>
</head>
<body>
<button>显示当前系统时间</button>
<div>某个时间</div>
<p>直接显示时间</p>
<script>
//1.获取元素
var btn = document.querySelector('button');
var div = document.querySelector('div');
//2. 注册事件
btn.onclick = function() {
div.innerText = getDate();
}
function getDate() {
var date = new Date()
var year = date.getFullYear();
var month = date.getMonth() + 1;
var dates = date.getDate();
var arr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
var day = date.getDay();
return '今天是:' + year + '年' + month + '月' + dates + '日' + arr[day];
}
//直接显示
var p = document.querySelector('p');
p.innerText = getDate();
</script>
</body>
</html>
09-06
8900
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-04
5410
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-30
3336
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-12
5409
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-17
529
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-09
975
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-28
2684
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)