如何写一个js指定时间倒计时

2022-07-15 16:34:22 浏览数 (1)

代码语言:javascript复制
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>计时</title>
    </head>
    <body>
        <div style="display: flex; justify-content: center; align-items: center;">
            <span>从2019年6月31日至今:</span>
            <span id="timeid1" style="font-size:35px"></span>天
            <span id="timeid2" style="font-size:35px"></span>小时
            <span id="timeid3" style="font-size:35px"></span>分
            <span id="timeid4" style="font-size:35px"></span>秒
        </div>
        <script type="text/javascript">
            function loveTime() {
                var the=new Date(2019,6,31)
                var date = new Date() - the
                var days = Math.floor(date / 1000 / 60 / 60 / 24) // 从这里开始
                var daysmod = date - days * 24 *60 * 60 * 1000
                var hours = Math.floor(daysmod / 1000 / 60 / 60)
                var hoursmod = date - (days * 24 *60 * 60 * 1000) - (hours * 1000 * 60 * 60)
                var minutes = Math.floor(hoursmod / 1000 / 60)
                var minutesmod = date - (days * 24 *60 * 60 * 1000) - (hours * 1000 * 60 * 60) - (minutes * 1000 * 60)
                var seconds = Math.floor(minutesmod / 1000) // 到这里结束,是将总毫秒转化成对应天数 小时数 分钟数 秒数的转换方法
                var daysshow = document.getElementById("timdid1");
                var ds = document.getElementById("timeid1");
                var hs = document.getElementById("timeid2");
                var ms = document.getElementById("timeid3");
                var ss = document.getElementById("timeid4");
                ds.innerHTML = days;
                hs.innerHTML = hours;
                ms.innerHTML = minutes;
                ss.innerHTML = seconds;
            }
            // 使用定时器实现每一秒写一次时间
            setInterval("loveTime();",1000);

        </script>
    </body>
    </html>

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120055675

0 人点赞