var count= countTime(‘2016-12-31‘);
var time=document.createElement(‘div‘);
time.id=‘time‘;
document.body.appendChild(time);
var showTime=document.getElementById("time");
showTime.innerHTML=count;
setInterval(function(){
count= countTime(‘2016-12-31‘);
showTime.innerHTML=count;
},1000);
function countTime(endtime){
var endT= endtime.replace(/-/g,"/");//字符串转化为日期
var endTime= new Date(endT);
var nowTime= new Date();
var t=endTime.getTime()-nowTime.getTime();
var d=0;
var h=0;
var m=0;
var s=0;
if(t>=0){
d=Math.floor(t/1000/60/60/24);
h=Math.floor(t/1000/60/60%24);
m=Math.floor(t/1000/60%60);
s=Math.floor(t/1000%60);
}
h=h<10?"0"+h:h;
m=m<10?"0"+m:m;
s=s<10?"0"+s:s;
return d+":"+h+":"+m+":"+s;
}