<!doctype html> <html> <head> <meta charset="utf-8"> <title>JS实现倒计时(时、分,秒)</title> <script> function ShowCountDown(year,month,day,divname) { var now = new Date(); var endDate = new Date(year, month, day); var leftTime=endDate.getTime()-now.getTime(); var dd = parseInt(leftTime / 1000 / 60 / 60 / 24, 10);//计算剩余的天数 var hh = parseInt(leftTime / 1000 / 60 / 60 % 24, 10);//计算剩余的小时数 var mm = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟数 var ss = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数 dd = checkTime(dd); hh = checkTime(hh); mm = checkTime(mm); ss = checkTime(ss);//小于10的话加0 var cc = document.getElementById(divname); cc.innerHTML = "距离" + year + "年" + month + "月" + day + "日还有:" + dd + "天" + hh + "小时" + mm + "分" + ss + "秒"; } function checkTime(i) { if (i < 10) { i = "0" + i; } return i; } window.setInterval(function(){ShowCountDown(2020,11,11,'countdown');}, 1000); </script> </head> <body> <div id="countdown"></div> </body> </html>