.box {
width: 200px;
height: 200px;
background-image: linear-gradient(pink, aqua);
border-radius: 10%;
text-align: center;
line-height: 200px;
}
<script>
const box = document.querySelector('.box')
function fn() {
const now = new Date() //
const yy = now.getFullYear() //年份
let MM = now.getMonth() + 1 //月份 0~11 ,0表示一月
let dd = now.getDate() //日期 1-31
let hh = now.getHours() //小时 0-23
let mm = now.getMinutes() //分钟 0-59
let ss = now.getSeconds() //秒 0-59
// 调用函数
MM = padZero(MM)
dd = padZero(dd)
hh = padZero(hh)
mm = padZero(mm)
ss = padZero(ss)
const str = `${yy}-${MM}-${dd} ${hh}:${mm}:${ss}`
box.innerHTML = str
}
fn()
setInterval(fn, 1000)
// 声明函数
function padZero(m) {
// 三元,小于10,+0
return m < 10 ? '0' + m : m
}