1 <script defer type="text/javascript"> 2 function settime() 3 { 4 var time = "2013/2/13/11/45/00"; /*这里设置日期,注意:time的格式 "年/月/日/时/分/秒"*/ 5 var date = time.split(‘/‘); 6 var date1 = new Date(); //目标日期 7 var date2 = new Date(); //当前时间 8 date1.setFullYear(date[0], (date[1]-1), date[2]); //设置目标日期的年,月,日 9 date1.setHours(date[3], date[4], date[5], 0); //设置目标日期的时,分,秒,毫秒 10 if (date1.getTime()<date2.getTime()){ 11 alert("活动结束"); 12 window.location.href="top.htm" 13 } 14 else 15 { 16 var differtime = parseInt((date1.getTime() - date2.getTime()) / 1000); 17 timeold=(date1.getTime()-date2.getTime()); 18 sectimeold=timeold/1000 19 secondsold=Math.floor(sectimeold); 20 msPerDay=24*60*60*1000 21 e_daysold=timeold/msPerDay 22 daysold=Math.floor(e_daysold); 23 e_hrsold=(e_daysold-daysold)*24; 24 hrsold=Math.floor(e_hrsold); 25 e_minsold=(e_hrsold-hrsold)*60; 26 minsold=Math.floor((e_hrsold-hrsold)*60); 27 seconds=Math.floor((e_minsold-minsold)*60); 28 29 var html="<b><align=center><font color=red>"+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒"+"</font>" ; 30 document.getElementById("span_dt_dt").innerHTML = html; 31 setTimeout("settime()", 1000); 32 } 33 } 34 </script>
body部分添加加载代码:
<body onload="settime()">
显示结果调用代码:
距离***还有:<span id="span_dt_dt" style="align: center no:1px
solid black"></span>