其他 · 2022年8月31日 0

利用JavaScript简单写一个站点计时器

站点计时器,用于站点底部统计运行时间。(纯JavaScript程序,暂不考虑闰年和闰月问题)

<center>      
  </span>
        <span class="nav-item">
        <span id="timeDate">载入时间中...</span><span id="times">载入失败,请刷新重试!</span> <script language="javascript"> 
    var now = new Date();
    function createtime(){
        var grt= new Date("09/11/2021 00:00:00");/*Initialization date*/
        now.setTime(now.getTime()+250);
        days = (now - grt ) / 1000 / 60 / 60 / 24;
        dnum = Math.floor(days);
        hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum);
        hnum = Math.floor(hours);
        if(String(hnum).length ==1 ){hnum = "0" + hnum;}
        minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
        mnum = Math.floor(minutes);
        if(String(mnum).length ==1 ){mnum = "0" + mnum;}
        seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
        snum = Math.round(seconds);
        if(String(snum).length ==1 ){snum = "0" + snum;}
        document.getElementById("timeDate").innerHTML = "⏱️本站已稳定运行"+dnum+"天";
        document.getElementById("times").innerHTML = hnum + "小时" + mnum + "分" + snum + "秒";
    }
    setInterval("createtime()",250); 
</script> 

效果展示:

载入时间中…载入失败,请刷新重试!