直接上代码:
禁止盗用;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width: 400px;
height: 400px;
margin-top: 200px;
margin-left: 35%;
}
</style>
<script>
function time() {
var nowTime = document.getElementById("time");
var day1 = new Date();
var day2 = new Date("7/8/2022");
var differ = day2.getTime()-day1.getTime();
var time = new Date(differ);
console.log(time);
nowTime.innerText=
(1970-time.getFullYear())+"-年 "+
time.getMonth()+"-月-" +
(time.getDate()-1)+"-天-"+ /* 因为默认时间是1970-1-1,得到的diff差是相减的时间截*/
time.getHours()+"-小时-"+ /*用Date转换后默认在1970基础上增加时间截(ms)所以减‘1’*/
time.getMinutes()+"-分钟-"+
time.getSeconds()+"-秒 ";
}
setInterval(time,1000);
</script>
</head>
<body>
<div>
距离2022-7-8-12:00还有:</br>
<span id="time" style="font-size: 21px"></span>
</div>
</div>
</body>
</html>