<!DOCTYPE html>
<html lang="en">
<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>
<style>
#da{
width: 500px;
height: 50px;
padding-top: 20px;
font: 900 20px '';
text-align: center;
border: 5px solid red;
}
</style>
</head>
<body>
<div id="app"></div>
<script>
let _app=document.getElementById("app");
//计时调用
let id=setInterval("test()",1000);
function test() {
let date = new Date(); //当前时间
let year = date.getFullYear(); //年
let month = date.getMonth() + 1; //月 0-11
month = (month + "").padStart(2, 0)
let day = date.getDate(); //日
day = (day + "").padStart(2, 0)
let week = date.getDay(); //星期
week=switchWeek(week);
let hours = date.getHours(); //小时
hours = (hours + "").padStart(2, 0)
let minutes = date.getMinutes(); //分钟
minutes = (minutes + "").padStart(2, 0);
let seconds = date.getSeconds(); //秒
seconds = (seconds + "").padStart(2, 0)
let str=switchTime(hours); //判断 am、pm
_app.innerHTML=`今天是${year}年${month}月${day}日
${week} ${hours}:${minutes}:${seconds} ${str}`
}
//星期转换
function switchWeek(num) {
switch (num) {
case 1:
return "星期一";
break;
case 2:
return "星期二";
break;
case 3:
return "星期三";
break;
case 4:
return "星期四";
break;
case 5:
return "星期五";
break;
case 6:
return "星期六";
break;
case 0:
return "星期日";
break;
}
}
//am、pm判断
function switchTime(hours){
if(hours>12){
return "pm";
}
return "am";
}
</script>
</body>
</html>