网站底部统计运行时间

许多建站的小伙伴都希望自己的网站有一个运行时间统计,可以给自己继续走下去多一些动力。所以我今天要分享一段兼容性很好的脚本代码,来实现此功能。话不多说,咋们直接上代码!

代码展示

<center>
   <span id="sitetime"></span>
</center>
<script language="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(2020,4,13,6,59,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()
 </script>

效果展示

已运行 0 年 196 天 8 小时 50 分钟 39 秒

代码美化

关于时间显示颜色:

<center>
   <span id="sitetime" style="color:代码颜色;"></span>
</center>

关于时间更改:

var t1 = Date.UTC(年,月,日,时,分,秒)
© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
评论 抢沙发

请登录后发表评论