原文网址:HTML--给网站添加已运行时间的统计_IT利刃出鞘的博客-CSDN博客
简介
本文介绍怎么给自己网站添加已经运行了多长时间。
代码
<html lang="cn"><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><title></title>
</head><body><span id="site-time"></span>
<script>function siteTime() {window.setTimeout("siteTime()", 1000);var seconds = 1000var minutes = seconds * 60var hours = minutes * 60var days = hours * 24var years = days * 365var today = new Date()var todayYear = today.getFullYear()var todayMonth = today.getMonth()var todayDate = today.getDate()var todayHour = today.getHours()var todayMinute = today.getMinutes()var todaySecond = today.getSeconds()var t1 = Date.UTC(2017, 2, 11, 0, 0, 0)var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond)var diff = t2 - t1var diffYears = Math.floor(diff / years)var diffDays = Math.floor((diff / days) - diffYears * 365)var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours)var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) / minutes)var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours - diffMinutes * minutes) / seconds)document.getElementById("site-time").innerHTML = "本站已稳定运行:"+ diffYears + " 年 " + diffDays + " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒"// document.getElementById("site-time").innerHTML = "本站已稳定运行:"// + (diffYears * 365 + diffDays) + " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒"}siteTime()</script></body></html>