代码收藏 / 建站笔记 · 2024年8月22日 0

给网站底部加上网站运行时间的JS代码

通常在我们建设网站的时候,时间一长就不清楚具体建设开始时间了,那么如何快捷添加网站运行时间代码呢 ?

下面分享使用方法,直接复制代码到相应位置,根据需要做合适调整即可

网站运行时间代码输出的时间,其秒数是动态变化的

<span id="span"></span>
<script type="text/javascript">
    function runtime(){
        // 初始时间,日/月/年 时:分:秒
        X = new Date("01/01/2024 00:00:00");
        Y = new Date();
        T = (Y.getTime()-X.getTime());
        M = 24*60*60*1000;
        a = T/M;
        A = Math.floor(a);
        b = (a-A)*24;
        B = Math.floor(b);
        c = (b-B)*60;
        C = Math.floor((b-B)*60);
        D = Math.floor((c-C)*60);
        //信息写入到DIV中
        span.innerHTML = "本网站己运行: "+A+"天"+B+"小时"+C+"分"+D+"秒"
    }
    setInterval(runtime, 1000);
</script>

<span id="timeDate"></span><span id="times"></span> 
<script language="javascript"> 
 var now = new Date();
 function createtime(){
  var grt= new Date("01/01/2024 00:00:00");/*---这里是网站时间--*/
  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>