Document * {padding: 0;margin: 0;}.box {width: 300px;height: 150px;background-color: #2e3344;color: white;margin: 300px auto;display: flex;justify-content: center;align-items: center;flex-direction: column;}h2 {text-align: center;font-size: 60px;}p {text-align: center;}span{font-size: 20px;margin-right: 15px;}
//获取对应的元素const div = document.querySelector('div')const h2 = document.querySelector('h2')const one = document.querySelector('.one')const two = document.querySelector('.two')const three = document.querySelector('.three')//封装倒计时函数,根据需求返结果function getTime() {let date = new Date()//当前时间let year = date.getFullYear()//当前年份let month = date.getMonth() + 1//当前月let day = date.getDate()//天数let week = date.getDay()//星期数let hour = date.getHours()//时hour = hour < 10 ? '0' + hour : hourlet minute = date.getMinutes()//分minute = minute < 10 ? '0' + minute : minutelet second = date.getSeconds()//秒second = second < 10 ? '0' + second : secondreturn ` ${hour}:${minute} `}h2.innerHTML = getTime()//获取当前时间的时,分//将星期数用数组包裹const arr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]setInterval(function () {h2.innerHTML = getTime()}, 1000)let date = new Date()one.innerHTML = `${date.getMonth() + 1}月${date.getDate()}日`//获取当前时间的月日two.innerHTML = `${arr[date.getDay()]}`//获取星期数