很多人都在各种博客网站底部看到过动态变化的运行时间,比如“本站点已运行100天21小时56分17秒”,那么要想添加这样一行内容其实非常简单,只要用到基础的JS。

在网站底部添加以下代码即可:

<span id="runtime"></span>
<script>function show_runtime(){window.setTimeout("show_runtime()",1000);X=new Date("6/29/2020 18:14:10");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);runtime.innerHTML="运行时间:"+A+"天"+B+"小时"+C+"分"+D+"秒"}show_runtime();</script>