倒计时

var timer=null;
var iNew=new Date(2018,10,23,16,19);//2017年11月24日14:55
//字符串形式
//var iNew=new Date('November 24,2017 14:55:0');
var iNow=new Date();//不断变换的时间
var t=Math.floor((iNew-iNow)/1000);//秒
var str=Math.floor(t/86400)+"天"+Math.floor(t%86400/3600)+"时"+Math.floor(t%86400%3600/60)+"分"+t%60+"秒";
if(iNew-iNow<0){
	document.body.innerHTML="0天0时0分0秒";
}else{
	document.body.innerHTML=str;
}

timer=setInterval(function(){
	var iNow=new Date();//不断变换的时间
	var t=Math.floor((iNew-iNow)/1000);//秒
	var str=Math.floor(t/86400)+"天"+Math.floor(t%86400/3600)+"时"+Math.floor(t%86400%3600/60)+"分"+t%60+"秒";
	if(iNew-iNow<0){
		clearInterval(timer);
	}else{
		document.body.innerHTML=str;
	}
},1000)
/*
 时间转换
1秒(s)=1000毫秒(ms)
1分(min)=60000毫秒(ms)
1时(h)=3600000毫秒(ms)
1天(d)=86400000毫秒(ms)

天:Math.floor(t/86400)
时:Math.floor(t%86400/3600)
分:Math.floor(t%86400%3600/60)
秒:t%60
 * */

  

原文地址:https://www.cnblogs.com/yangxue72/p/7886178.html