倒计时

js代码

var EndTime= new Date('2017/2/18 00:00:00'); //设置到期时间
    var NowTime = new Date();  //当前时间
    var t =EndTime.getTime() - NowTime.getTime();  //时间差
    var d=Math.floor(t/1000/60/60/24); //剩余天数
    var h=Math.floor(t/1000/60/60%24);  //剩余小时数
    var m=Math.floor(t/1000/60%60);  //剩余分钟天数
    var s=Math.floor(t/1000%60); //剩余秒数
    var getCodeTimer = setInterval(function() {
        t--;
        $('#t-d').html(d+'天');
        $('#t-h').html(h+'小时');
        $('#t-m').html(m+'分');
        if (t <= 0) {     //当时间差小于等于0时,归零
            clearInterval(getCodeTimer);
            $('#t-d').html(0+'天');
            $('#t-h').html(0+'小时');
            $('#t-m').html(0+'分');
        }
    }, 100);

第二种方式:

// 倒计时
    function GetRTime(){
       var EndTime= new Date('2017/3/19 00:00:00');
       var NowTime = new Date();
       var t =EndTime.getTime() - NowTime.getTime();
       var d=Math.floor(t/1000/60/60/24);
       var h=Math.floor(t/1000/60/60%24);
       var m=Math.floor(t/1000/60%60);
       var s=Math.floor(t/1000%60);
       $(".day").html(d);
       $(".hour").html(h);
       $(".minute").html(m);
       $(".second").html(s);
       if (t<=0) {
        clearInterval(GetRTime);
       }
   }
   setInterval(GetRTime,0);
原文地址:https://www.cnblogs.com/qiye2016/p/6405660.html