添加网站运行时间(JS实现)
Sonder
2021-08-23
730字
2分钟
浏览 (3k)
<p id="showTime" style="color:#000000;text-align:center;"></p>//这里相当于一个占位符,待会JS会在这里生成结果
<script>
function showTime(){
var birth = new Date('2021/05/24 13:00');//这里是起始时间
var cur = new Date();
var diff = (cur.getTime() - birth.getTime())/1000;//总时间差单位是秒
var day = parseInt(diff/86400);
var hour = parseInt((diff - day*86400)/3600);
var min = parseInt((diff - day*86400 - hour*3600)/60);
var second = parseInt((diff - day*86400 - hour*3600 - min*60));
document.getElementById("showTime").innerHTML = "本站已运行了 " + day + "天" + hour + "小时" + min + "分钟" + second + "秒";//将结果写在开头的“占位符”p中
}
setInterval(showTime,1000);//每一秒执行一次该函数
</script>