创建siteTime.js文件并连接到网站
代码语言:javascript复制function siteTime() {
window.setTimeout("siteTime()", 1000);
var seconds = 1000
var minutes = seconds * 60
var hours = minutes * 60
var days = hours * 24
var years = days * 365
var 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(2016, 8, 27, 08, 19, 00) /*这里修改为建站时间*/
var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond)
var diff = t2 - t1
var 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("sitetime").innerHTML = "" diffYears " 年 " diffDays " 天 " diffHours " 小时 " diffMinutes " 分钟 " diffSeconds " 秒"
}
siteTime()
将下列代码放在你想要显示网站运行时间的位置即可
代码语言:javascript复制<p>本站已稳定运行<span id="sitetime" ></span></p>