1 var countDown = function (){ 2 var date1 = new Date("2015/4/9"); //目标日期 注意字符串 /格式 -格式IE不支持 3 var date2 = new Date();//当前日期 4 5 var times = date1.getTime() - date2.getTime();//相差毫秒数 6 7 var days = Math.floor(times/(1000*60*60*24)) ///剩余天数 8 var rest1 = times%(1000*60*60*24); //取余数 9 10 var hours = Math.floor(rest1/(1000*60*60)); 11 var rest2 = rest1%(1000*60*60); 12 13 var minutes = Math.floor(rest2/(1000*60)); 14 var rest3 = rest2%(1000*60); 15 16 var seconds = Math.round(rest3/(1000));///最后秒数四舍五入就可以了 17 18 var zero = function (num){ 19 return num < 10 ? "0" + num : num; 20 }; 21 22 document.body.innerHTML = 23 '倒计时:' 24 + days + "天 " 25 + zero(hours) + "时 " 26 + zero(minutes) + "分 " 27 + zero(seconds) + "秒 "; 28 29 } 30 countDown(); 31 setInterval(countDown,1000);