简单倒计时
2017-09-13 本文已影响0人
星球小霸王
<div id="box"></div>
<script>
var box = document.getElementById("box");
var span = document.getElementById("span");
var end,now,allTime,days,hours,minutes,seconds,millise,time;
time = "2017-9-19 13:21:00:00";
end = new Date(time);
function curstor(){
now = new Date();
allTime = end.getTime()-now.getTime();
days = Math.floor(allTime/1000/60/60/24);//求出整的天数
hours = Math.floor((allTime/1000/60/60)%24)//求出所有的小时数,然后取模求出不足一天的小时数;
minutes = Math.floor((allTime/1000/60)%60);//求出整的分钟数,然后取模求出不足一小时的分钟数
seconds = Math.floor((allTime/1000)%60);//求出整的秒数,然后取模求出不足一分钟的秒数
millise = Math.floor((allTime)%60);//求出所有的秒数,然后取模求出不足一秒的毫秒数;
if(hours<10){
hours="0"+hours;
}
if(minutes<10){
minutes="0"+minutes;
}
if(seconds<10){
seconds="0"+seconds;
}
if(millise<10){
millise="0"+millise;
}
box.innerHTML="距离"+time+"还剩下:"+days+"天"+hours+"小时"+minutes+"分钟"+seconds+"秒"+millise+"毫秒";
// console.log(allTime);
if(allTime<0){
clearInterval(box.t);
days=0;
hours=0;
minutes=0;
seconds=0;
millise=0;
box.innerHTML="距离"+time+"还剩下:"+days+"天0"+hours+"小时0"+minutes+"分钟0"+seconds+"秒0"+millise+"毫秒";
}
}
// curstor();
box.t = setInterval("curstor()",0);
</script>