1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <title>生日倒计时</title> 6 <style type="text/css"> 7 #msg{ 8 text-align: center; 9 font-size: 20px; 10 } 11 12 #timer{ 13 font-size: 20px; 14 color: red; 15 } 16 </style> 17 <script type="text/javascript"> 18 19 function showTime(){ 20 var currentTime=new Date(); 21 var endTime=new Date("2018/2/13"); 22 var remainTime=endTime.getTime()-currentTime.getTime(); 23 // 1天=24小时 1小时=60分钟 1分钟=60秒 1秒=1000毫秒 24 var day=Math.floor(remainTime/(24*60*60*1000)); 25 var hour=Math.floor((remainTime-day*24*60*60*1000)/(60*60*1000)); 26 var minute=Math.floor((remainTime-day*24*60*60*1000-hour*60*60*1000)/(60*1000)); 27 var second=Math.floor((remainTime-day*24*60*60*1000-hour*60*60*1000-minute*60*1000)/(1000)); 28 if(remainTime<0){ 29 document.getElementById("msg").innerHTML="生日已经结束!"; 30 }else{ 31 var time=day+"天 "+hour+"时"+minute+"分"+second+"秒!"; 32 document.getElementById("timer").innerHTML=time; 33 } 34 35 } 36 setInterval("showTime()",1000); 37 </script> 38 </head> 39 <body> 40 <div id="msg">距离生日还有:<br/><span id="timer"></span></div> 41 </body> 42 </html>