经常遇到需要倒计时的地方,下面的JS可以简单实现倒计时,当然可以加上Css使得界面更加美观!
<script type=
"text/javascript"
>
function
CountDown(){
var
EndTime=
new
Date(document.getElementById('endTime').value
);
var
NowTime =
new
Date();
var
t =EndTime.getTime() - NowTime.getTime();
var
day=Math.floor(t/1000/60/60/24);
var
hour=Math.floor(t/1000/60/60%24);
var
min=Math.floor(t/1000/60%60);
var
sec=Math.floor(t/1000%60);
document.getElementById(
"t_day"
).innerHTML = day +
"Days"
;
document.getElementById(
"t_hour"
).innerHTML = hour +
"Hours"
;
document.getElementById(
"t_min"
).innerHTML = min +
"Minutes"
;
document.getElementById(
"t_sec"
).innerHTML = sec +
"Seconds"
;
}
setInterval(CountDown,1000);
</script>
</head>
<body>
Please select the endTime<input type="datetime-local" id='endTime'><br>
<div
>
<span id=
"t_day"
></span>
<span id=
"t_hour"
></span>
<span id=
"t_min"
></span>
<span id=
"t_sec"
></span>
</div>
</body>