<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>倒计时</title> </head> <body> <script> // 格式化00 function forMat(t) { return t < 10 ? "0" + t : t } function countDown(time) { var currentTime = +new Date() var futureTime = new Date(time) var times = futureTime - currentTime times = times / 1000 //得到时间戳-秒 var d = forMat(parseInt(times / 60 / 60 / 24)) // 天 var h = forMat(parseInt(times / 60 / 60 % 24)) // 时 var s = forMat(parseInt(times / 60 % 60)) // 分 var m = forMat(parseInt(times % 60)) // 秒 return d + "天" + h + "时" + s + "分" + m + "秒" } console.log(countDown("2020-5-4 00:00:00")); </script> </body> </html>