js实现在html5页面显示倒计时(天时分秒)

html代码(简化):

<b id="time_b">b>

js代码:

function ShowCountDown(jztime,divname) {
    var now = new Date();
    var endDate = new Date(Date.parse(jztime.replace(/-/g, "/")));
    var leftTime=endDate.getTime()-now.getTime();
    var leftsecond = parseInt(leftTime/1000);
    var day1=Math.floor(leftsecond/(60*60*24));
    var hour=Math.floor((leftsecond-day1*24*60*60)/3600);
    var minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60);
    var second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60);
    if(day1 < 10){day1 = "0"+day1;}
    if(hour < 10){hour = "0"+hour;}
    if(minute < 10){minute = "0"+minute;}
    if(second < 10){second = "0"+second;}
    var cc = document.getElementById(divname);
    cc.innerHTML = "剩余:"+day1+""+hour+""+minute+""+second+"";
}
var engTime = "2018-5-2";//截止时间
window.setInterval(function(){ShowCountDown(engTime,'time_b');}, 1000);

你可能感兴趣的:(js)