效果显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
h1 {
margin: 200px auto;
text-align: center;
}
span {
color: purple;
}
</style>
</head>
<body>
<h1 style="color:pink">
2020新年倒计时: <span></span> 天 <span></span> 小时 <span></span> 分 <span></span> 秒
</h1>
<script>
var goalDate = new Date(2020, 0, 24);
var span = document.getElementsByTagName('span');
// 将单数改为双数(6 ~ 06)
function toTwo(params) {
// console.log(params);
if (params < 10) {
return '0' + params
// console.log(params);
} else {
return params
}
}
function gapTime() {
var localTime = new Date();
gap_time = goalDate - localTime
gap_day = parseInt(gap_time / 1000 / 60 / 60 / 24);
gap_day = toTwo(gap_day);
gap_hour = parseInt(gap_time / 1000 / 60 / 60 % 24);
gap_hour = toTwo(gap_hour);
gap_minute = parseInt(gap_time / 1000 / 60 % 60);
gap_minute = toTwo(gap_minute);
gap_seconds = parseInt(gap_time / 1000 % 60);
gap_seconds = toTwo(gap_seconds);
span[0].innerHTML = gap_day;
span[1].innerHTML = gap_hour;
span[2].innerHTML = gap_minute;
span[3].innerHTML = gap_seconds;
}
gapTime();
setInterval(gapTime, 1000);
</script>
</body>
</html>
来源:CSDN
作者:玩爬虫的小朋友
链接:https://blog.csdn.net/gklcsdn/article/details/104040484