commitTimeDate = new Date("2016/11/9 10:02:40").getTime() + 24*60*60*1000;//截止时间 myDate = new Date();//获取当前时间 currentTime=myDate.getTime();//当前时间换成毫秒 deadlineTime = commitTimeDate - currentTime;//时间差 let reminderHour = Math.floor(deadlineTime / (1000 * 60 * 60));//倒计时要写的小时 let reminderSecond = Math.floor(deadlineTime / (1000 * 60)- reminderHour*60);//倒计时要填的分钟 let reminderMinute = Math.floor(deadlineTime / 1000 - reminderSecond*60 - reminderHour*60*60);//倒计时要填的秒
jQuery:
<body> <div class="time"> <span class="days"></span>days: <span class="h"></span>hour: <span class="s"></span>sec: <span class="m"></span>min </div> <script type="text/javascript" src="../js/jquery/jquery-1.11.3.min.js" ></script> <script type="text/javascript"> function GetRTime(){ commitTimeDate = new Date("2017/03/15 00:00:00").getTime() + 24*60*60*1000;//截止时间 myDate = new Date();//获取当前时间 currentTime=myDate.getTime();//当前时间换成毫秒 deadlineTime = commitTimeDate - currentTime;//时间差 var reminderHour = Math.floor(deadlineTime / (1000 * 60 * 60));//倒计时要写的小时 var days = parseInt(reminderHour/24); var hours = reminderHour%24; var reminderSecond = Math.floor(deadlineTime / (1000 * 60)- reminderHour*60);//倒计时要填的分钟 var reminderMinute = Math.floor(deadlineTime / 1000 - reminderSecond*60 - reminderHour*60*60);//倒计时要填的秒 $(".days").html(days); $(".h").html(hours); $(".s").html(reminderSecond); $(".m").html(reminderMinute); } //GetRTime(); setInterval(GetRTime,0); </script> </body>
JavaScript:
<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 EndTime= new Date('2017/09/20 00:00:00'); var NowTime = new Date(); 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>