时间倒计时 JS

<div id="keleyi">Christmas Countdown</div>

<script type="text/javascript">
function iCounter() {
    var myDate = (new Date().getMonth()>=11 && new Date().getDate()>29)?(new Date().getFullYear())+1:(new Date().getFullYear());//如果当前月份大于11月并且天数大于29就让当前年份+1否则当前年份不变;
    var date = new Date();
    var tDate = new Date("April 28, "+myDate+" 0:0:00");
    var timeDifference = (tDate - date)/1000;//时间差
    var oneMin = 60; //天 时 分 都换算成秒
    var oneHour = 60*60;
    var oneDay = 60*60*24;
    var totalDay = Math.floor(timeDifference/oneDay);
    var totalHour = Math.floor((timeDifference-totalDay*oneDay)/oneHour);
    var totalMin = Math.floor((timeDifference - totalDay*oneDay -totalHour*oneHour)/oneMin);
    var totalSec = Math.floor((timeDifference - totalDay*oneDay - totalHour*oneHour - totalMin*oneMin));
    if(totalDay == 0){ totalDay = ''}else{totalDay};
    document.getElementById('kel' + 'eyi').innerHTML = '28日<br/>' + totalDay  + totalHour + '<span>时,</span>' + totalMin + '<span>分,</span>' + totalSec + '<span>秒。</span>';
    setTimeout(iCounter,1000);
}
iCounter();

</script>
原文地址:https://www.cnblogs.com/fanxiaowu/p/4460233.html