jquery 传入时间戳 显示倒计时

浦出野
2023-12-01
 <p id="countDown1"></p>
    <script type="text/javascript" src="node_modules/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript">
        function getCountDown(timestamp){
            var timer = setInterval(function(){
                var nowTime = new Date();
                var endTime = timestamp;
 
                var t = endTime - nowTime.getTime();
 
                if(t <= 0){
                    $("#countDown1").html('over');
                    clearInterval(timer);
                    return false;
                }
                // var d=Math.floor(t/1000/60/60/24);
                var hour=Math.floor(t/1000/60/60%24);
                var min=Math.floor(t/1000/60%60);
                var sec=Math.floor(t/1000%60);
 
                if (hour < 10) {
                     hour = "0" + hour;
                }
                if (min < 10) {
                     min = "0" + min;
                }
                if (sec < 10) {
                     sec = "0" + sec;
                }
                var countDownTime = hour + ":" + min + ":" + sec;
                $("#countDown1").html(countDownTime);
 
            },1000);
        }
 
        var date = '2018-11-02 10:33:00.0';
        date = date.substring(0,19);    
        date = date.replace(/-/g,'/'); 
        var timestamp = new Date(date).getTime(); // 毫秒级
 
        getCountDown(timestamp);
    </script>

 

 类似资料: