1.实现:
在页面输出系统时间
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p></p>
<script>
setInterval(function() {
// 1.日期对象(系统时间)
let date = new Date()
// 提取年月日时分秒
let year = date.getFullYear()
let month = date.getMonth() + 1 //月份从0-11
let day = date.getDate()
let hours = date.getHours()
let minutes = date.getMinutes()
let seconds = date.getSeconds()
// 判断时间不够两位,前面补零
if(month < 10) {
month = '0' + month
}
if(day < 10) {
day = '0' + day
}
hours = hours < 10 ? '0' + hours : hours
minutes = minutes < 10 ? '0' + minutes : minutes
seconds = seconds < 10 ? '0' + seconds : seconds
// 拼接数字
let time = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
document.querySelector('p').innerHTML = time
},1000)
</script>
</body>
</html>