<!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>
#box {
width: 500px;
height: 500px;
background: #eee;
margin: 20px auto;
text-align: center;
}
#box h3 {
font-size: 32px;
padding: 30px;
color: blue;
}
#box input {
width: 100px;
}
button {
width: 160px;
height: 160px;
background: #000;
margin-top: 30px;
border-radius: 80px;
color: yellow;
font-size: 40px;
}
.p1 span {
font-size: 24px;
color: blue;
}
.p2 {
font-size: 30px;
color: rgb(247, 7, 47);
}
.p2 span {
font-size: 40px;
color: blue;
}
</style>
</head>
<body>
<div id="box">
<h3>倒计时</h3>
请输入:<input type="text" value="2022" />年
<input type="text" value="12" />月
<input type="text" value="12" />日
<button>开始</button>
<p class="p1">现在距离- <span>0000</span> -还剩:</p>
<p class="p2"><span>00</span>天 <span>00</span>小时 <span>00</span>分 <span>00</span>秒</p>
</div>
</body>
<script>
var oIpt = document.getElementsByTagName('input');
var oBtn = document.getElementsByTagName('button')[0];
var oSpan = document.getElementsByTagName('span');
oBtn.onclick = function () {
add();
setInterval(add, 1000);
function add() {
oSpan[0].innerHTML = oIpt[0].value + '年' + oIpt[1].value + '月' + oIpt[2].value + '日';
var oNew = new Date();
var oOld = new Date(oIpt[0].value, oIpt[1].value - 1, oIpt[2].value);
console.log(oOld);
var time = parseInt((oOld - oNew) / 1000);
console.log(time);
var t = parseInt(time / 86400);
console.log(t);
var s = parseInt(time % 86400 / 3600);
console.log(s);
var f = parseInt(time % 86400 % 3600 / 60);
console.log(f);
var m = time % 60;
console.log(m);
oSpan[1].innerHTML = t;
oSpan[2].innerHTML = s;
oSpan[3].innerHTML = f;
oSpan[4].innerHTML = m;
}
}
</script>
</html>