<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>限时抢</title> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript"> window.onload = function(){ showTime(); } function showTime(){ var endTime = new Date("2017/7/15,9:30:00"); var startTime = new Date(); var leftSeconds = parseInt((endTime.getTime()-startTime.getTime())/1000);//两个时间点之间的秒数 var d = parseInt(leftSeconds/(60*60*24));//天数,这个直接取整 var h = parseInt(leftSeconds/3600)%24;//注意这里,小时需要求余24,为了获取天数之外的小时数 var m = parseInt(leftSeconds/60)%60;//只获取残留的分钟数 var s = leftSeconds%60;//残留的秒数 if(leftSeconds<=0){ document.getElementById("LeftTime").innerHTML="团购已结束"; }else{ document.getElementById("LeftTime").innerHTML=d+"天"+h+"小时"+m+"分"+s+"秒"; setTimeout(showTime,500); } } </script> </head> <!-- 计算距离某个时间点的多少天多少小时多少分多少秒 --> <body> <div class="content3"> <div class="time">还剩 <span id="LeftTime"></span></div> </div> </body> </html>
效果: