活动倒计时范例(日期倒计时例子)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>倒计时</title>
</head>
<body>
<p id="demo"></p>
<script>
// 设置咱们准备倒计时的日期
const countDownDate = new Date("2021-11-30 24:00:00").getTime();
// 每秒更新一次
var x = setInterval(function() {
// 获取当前时间
var now = new Date().getTime();
// 获取当前时间与countDownDate 的时间差
var distance = countDownDate - now;
// 将时间差转为 days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("demo").innerHTML = days + "天 " + hours + ":"
+ minutes + ":" + seconds + "";
// 若是时间差耗尽,结束定时器,秒杀结束
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "提示:活动已经结束。";
}
}, 1000);
</script>
</body>
</html>
//来源:http://cn.voidcc.com/question/p-mjepyoil-hk.html