1 <div id="times_wrap" class="time_num">
2 距离结束时间:
3 <div class="time_w">
4 <b id="times_d" class="time"> </b>天
5 <b id="times_h" class="time"> </b>时
6 <b id="times_m" class="time"> </b>分
7 <b id="times_s" class="time"> </b>秒
8 </div>
9 </div>
1 var time_wrap = document.getElementById("times_wrap");
2 var time_d = document.getElementById("times_d");
3 var time_h = document.getElementById("times_h");
4 var time_m = document.getElementById("times_m");
5 var time_s = document.getElementById("times_s");
6
7 var time_end = new Date("2015/9/30 18:00:00"); // 设定结束时间
8 time_end = time_end.getTime();
9
10 function show_time(){
11 var time_now = new Date(); // 获取当前时间
12 time_now = time_now.getTime();
13 var time_distance = time_end - time_now; // 结束时间减去当前时间
14 var int_day, int_hour, int_minute, int_second;
15 if(time_distance >= 0){
16 // 天时分秒换算
17 int_day = Math.floor(time_distance/86400000)
18 time_distance -= int_day * 86400000;
19 int_hour = Math.floor(time_distance/3600000)
20 time_distance -= int_hour * 3600000;
21 int_minute = Math.floor(time_distance/60000)
22 time_distance -= int_minute * 60000;
23 int_second = Math.floor(time_distance/1000)
24
25 // 时分秒为单数时、前面加零站位
26 if(int_hour < 10)
27 int_hour = "0" + int_hour;
28 if(int_minute < 10)
29 int_minute = "0" + int_minute;
30 if(int_second < 10)
31 int_second = "0" + int_second;
32
33 // 显示时间
34 time_d.innerHTML = int_day;
35 time_h.innerHTML = int_hour;
36 time_m.innerHTML = int_minute;
37 time_s.innerHTML = int_second;
38 }else{
39 time_d.innerHTML = time_d.innerHTML;
40 time_h.innerHTML = time_h.innerHTML;
41 time_m.innerHTML = time_m.innerHTML;
42 time_s.innerHTML = time_s.innerHTML;
43 }
44 };
45 window.setInterval(function(){
46 show_time();
47 }, 1000);