倒计时js代码
倒计时js代码精确到时分秒,使用方法:注意 var EndTime= new Date('2013/05/1 10:00:00'); //截止时间 这一句,特别是 '2013/05/1 10:00:00' 这个js日期格式一定要注意,否则在IE6、7下工作计算不正确哦。
js代码如下:
function GetRTime(){
var EndTime= new Date('2013/05/1 10:00:00'); //截止时间
var NowTime = new Date();
var t =EndTime.getTime() - NowTime.getTime();
/*var d=Math.floor(t/1000/60/60/24);
t-=d*(1000*60*60*24);
var h=Math.floor(t/1000/60/60);
t-=h*60*60*1000;
var m=Math.floor(t/1000/60);
t-=m*60*1000;
var s=Math.floor(t/1000);*/
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);
还要一种好多if
function timeRun(hour,minute,second){ var hour = hour, minute = minute, second = second; var timer = setInterval(function(){ second--; if (second<0) { second = 60; $(".second").text(second); minute--; if (minute<0) { minute = 60; $(".second").text(minute); hour--; if (hour<0) { $(".second").text("00"); //alert("倒计时结束!") clearInterval(timer); }else if (hour<10) { $(".hour").text("0"+hour); }else{ $(".hour").text(hour); } }else if (minute<10) { $(".minute").text("0"+minute); }else{ $(".minute").text(minute); }; }else if (second<10) { $(".second").text("0"+second); }else{ $(".second").text(second); }; },1000) }
还有一种好像更好
HTML代码: <div id="times_wrap" class="time_num"> 距离结束时间: <div class="time_w"> <b id="times_d" class="time"> </b>天 <b id="times_h" class="time"> </b>时 <b id="times_m" class="time"> </b>分 <b id="times_s" class="time"> </b>秒 </div> </div> JavaScript代码: <script type="text/javascript"> setTimeout("show_time()",1000); var time_wrap = document.getElementByIdx_x("times_wrap"); var time_d = document.getElementByIdx_x("times_d"); var time_h = document.getElementByIdx_x("times_h"); var time_m = document.getElementByIdx_x("times_m"); var time_s = document.getElementByIdx_x("times_s"); var time_end = new Date("2012/10/29 08:00:00"); // 设定结束时间 time_end = time_end.getTime(); function show_time(){ var time_now = new Date(); // 获取当前时间 time_now = time_now.getTime(); var time_distance = time_end - time_now; // 结束时间减去当前时间 var int_day, int_hour, int_minute, int_second; if(time_distance >= 0){ // 天时分秒换算 int_day = Math.floor(time_distance/86400000) time_distance -= int_day * 86400000; int_hour = Math.floor(time_distance/3600000) time_distance -= int_hour * 3600000; int_minute = Math.floor(time_distance/60000) time_distance -= int_minute * 60000; int_second = Math.floor(time_distance/1000) // 时分秒为单数时、前面加零站位 if(int_hour < 10) int_hour = "0" + int_hour; if(int_minute < 10) int_minute = "0" + int_minute; if(int_second < 10) int_second = "0" + int_second; // 显示时间 time_d.innerHTML = int_day; time_h.innerHTML = int_hour; time_m.innerHTML = int_minute; time_s.innerHTML = int_second; setTimeout("show_time()",1000); }else{ time_d.innerHTML = time_d.innerHTML; time_h.innerHTML = time_h.innerHTML; time_m.innerHTML = time_m.innerHTML; time_s.innerHTML = time_s.innerHTML; // clearTimeout(timerID) } }; </script>