js倒计时

<!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>
原文地址:https://www.cnblogs.com/zwnsyw/p/12589625.html