
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态时间</title>
<style>
.bigDiv{
margin: 50px auto;
width: 400px;
height: 100px;
background-color: lightskyblue;
border-radius: 10px;
}
.clockDiv{
background-color: orange;
border: solid olivedrab 2px;
width: 250px;
height: 30px;
text-align: center;
margin: 20px auto;
}
</style>
</head>
<div class="bigDiv">
<h2 align="center">当前时间</h2>
<div class="clockDiv" id="timeDiv">
</div>
</div>
<body onload="time()">
<script>
function time() {
showTime();
setInterval(showTime,1000);
}
function showTime() {
let date = new Date();
let year = date.getFullYear();
let month = appendZero(date.getMonth()+1);
let day = appendZero(date.getDate());
let hour = appendZero(date.getHours());
let min = appendZero(date.getMinutes());
let second = appendZero(date.getSeconds());
let week = date.getDay();
function appendZero(num){
if (num<10){
return "0"+num;
}
return num;
}
let curtime=year+"-"+month+"-"+day+" "+hour+":"+min+":"+second+" 星期"+week;
document.getElementById("timeDiv").innerHTML=curtime;
}
</script>
</body>
</html>