https://jsfiddle.net/5og49Lah/1/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div id="showtime"></div> <h1>My First Heading</h1> <p>My first paragraph.</p> <script> var showtime = function () { var nowtime = new Date(), //获取当前时间 endtime = new Date("2022/8/31"); //定义结束时间 var lefttime = endtime.getTime() - nowtime.getTime(), //距离结束时间的毫秒数 leftd = Math.floor(lefttime/(1000*60*60*24)), //计算天数 lefth = Math.floor(lefttime/(1000*60*60)%24), //计算小时数 leftm = Math.floor(lefttime/(1000*60)%60), //计算分钟数 lefts = Math.floor(lefttime/1000%60); //计算秒数 return "距离本次活动结束还剩"+leftd + "天" + lefth + "时" + leftm + "分" + lefts + "秒"; //返回倒计时的字符串 } var div = document.getElementById("showtime"); setInterval (function () { div.innerHTML = showtime(); }, 1000); //反复执行函数本身 </script> </body> </html> |