第一种:有开始时间和结束时间的
<!doctype html> <html> <head> <meta charset="utf-8"> <title>倒计时js代码</title> </head> <body> <DIV id="CountMsg" class="HotDate"> <span id="t_d">00天</span> <span id="t_h">00时</span> <span id="t_m">00分</span> <span id="t_s">00秒</span> </DIV> <script type="text/javascript"> function getRTime(){ var EndTime= new Date('2013/05/1 10:00:00'); //截止时间
//这是从回台传过来的结束时间值-----start
var endtime = '<%=dk_endTime %>';
var EndTime = new Date(endtime.replace(/-/g, "/")); //截止时间
//这是从回台传过来的结束时间值-----end var NowTime = new Date(); var t =EndTime.getTime() - NowTime.getTime(); var d=Math.floor(t/1000/60/60/24); var h=Math.floor(t/1000/60/60%24); var m=Math.floor(t/1000/60%60); var s=Math.floor(t/1000%60);
document.getElementById("t_d").innerHTML = d + "天"; document.getElementById("t_h").innerHTML = h + "时"; document.getElementById("t_m").innerHTML = m + "分"; document.getElementById("t_s").innerHTML = s + "秒"; } setInterval(getRTime,1000); </script> </body> </html>
第二种是没有开始时间和结束时间的
<!doctype html> <html> <head> <title>倒计时js代码</title> </head> <body> <script type="text/javascript"> var maxtime = 60 * 60 //一个小时,按秒计算,自己调整! function CountDown() { if (maxtime >= 0) { minutes = Math.floor(maxtime / 60); seconds = Math.floor(maxtime % 60); minutes = minutes >= 10 ? minutes : '0' + minutes; seconds = seconds >= 10 ? seconds : '0' + seconds; msg = "距离结束还有" + minutes + "分" + seconds + "秒"; document.all["timer"].innerHTML = msg; if (maxtime == 5 * 60) alert('注意,还有5分钟!'); --maxtime; } else { clearInterval(timer); alert("时间到,结束!"); } } timer = setInterval("CountDown()", 1000); </script> </body> <div id="timer" style="color:red"></div> </html>
请自测试,没问题啦!
var endtime = '<%=dk_endTime %>';
var EndTime = new Date(endtime.replace(/-/g, "/")); //截止时间