<!doctype html> <html> <head> <meta charset="utf-8"> <title>简单易用的倒计时js代码</title> <style> *{ margin:0; padding:0; list-style:none;} body{ font-size:18px; text-align:center;} .time{ height:30px; padding:200px;} </style> </head> <body> <div class="time"> <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> function GetRTime() { var now = new Date(2016,9,1,0,0,0); var year=now.getFullYear(); var month=now.getMonth(); var urodz = new Date(parseInt(year),6,1,0,0,0); if(parseInt(month)>6) urodz=new Date(parseInt(year)+1,6,1,0,0,0); var NowTime = new Date(); var year=NowTime.getFullYear(); //获取完整的年份(4位,1970-????) var month=NowTime.getMonth(); //获取当前月份(0-11,0代表1月) var EndTime= new Date(parseInt(year),6,1,0,0,0); // if(parseInt(month>6) EndTime=new Date(parseInt(year)+1,6,1,0,0,0);} var t =EndTime.getTime() - NowTime.getTime(); var d=0; var h=0; var m=0; var s=0; if(t>=0){ d=Math.floor(t/1000/60/60/24); h=Math.floor(t/1000/60/60%24); m=Math.floor(t/1000/60%60); 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,0); </script> </body> </html> 这种方法 有一定问题 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>实现时间倒计时的JS代码</title> <style type="text/css"> </style> </head> <body> <div> <script language=JavaScript> function GetServerTime() { //var now = new Date(2016,5,1,0,0,0);// 给个上半年时间测试 var now = new Date(); var year=now.getFullYear(); var urodz = new Date(parseInt(year),6,1,0,0,0); var month=now.getMonth(); if(parseInt(month)>=6) urodz=new Date(parseInt(year)+1,6,1,0,0,0); days = (urodz - now) / 1000 / 60 / 60 / 24; daysRound = Math.floor(days); hours = (urodz - now) / 1000 / 60 / 60 - (24 * daysRound); hoursRound = Math.floor(hours); minutes = (urodz - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound); minutesRound = Math.floor(minutes); seconds = (urodz - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound); secondsRound = Math.round(seconds); document.getElementById("date").innerHTML = daysRound; document.getElementById("time").innerHTML = hoursRound + ":" + minutesRound + ":" + secondsRound;//+wznow;// } setInterval("GetServerTime()",250); </script> <span>距离毕业还有</span> <span id="date"></span> <span class="white14b">天</span> <span id="time"></span> </div> </body> </html> 时间是7月1日毕业 ,然后呢,表达时间为2017,6,1 测试 猎豹 IE均通过 效果理想
时间: 2024-10-06 00:23:36