<!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>倒计时</title>
<style>
.day,.hour,.minute,.second{
width: 100px;
height: 100px;
line-height: 100px;
background: rgb(202, 11, 11);
color: #fff;
float: left;
margin: 20px;
font-size: 24px;
text-align: center;
}
p{
float: left;
height: 100px;
line-height: 80px;
display: block;
font-size: 30px;
font-weight: bolder;
}
</style>
</head>
<body>
<div>
<div class="day"></div>
<p>:</p>
<div class="hour"></div>
<p>:</p>
<div class="minute"></div>
<p>:</p>
<div class="second"></div>
</div>
</body>
<script>
var day=document.querySelector(".day")
var hour=document.querySelector(".hour")
var minute=document.querySelector(".minute")
var second=document.querySelector(".second")
var setTime = +new Date("2021-7-16 20:00:00");
getCountTimer();
setInterval(getCountTimer, 1000);
function getCountTimer(){
var nowTime = +new Date();
var yuTime= ( setTime - nowTime ) / 1000;
var d=parseInt(yuTime / 60 / 60 / 24); // 计算天数
d = d < 10 ? "0"+ d : d;
day.innerHTML = d;
var h=parseInt(yuTime/ 60 / 60 % 24) // 计算小时
h = h < 10 ? "0"+ h : h;
hour.innerHTML= h;
var m=parseInt(yuTime / 60 % 60 ); // 计算分数
m = m < 10 ? "0"+ m : m;
minute.innerHTML= m;
var s=parseInt(yuTime % 60); // 计算当前秒数
s = s < 10 ? "0"+ s : s;
second.innerHTML = s;
}
</script>
</html>
|