1. 程式人生 > >js實現網站執行時間統計

js實現網站執行時間統計

這裡寫圖片描述

<span id="sitetime"></span>
<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()+1; var todayDate = today.getDate(); var todayHour = today.getHours(); var todayMinute = today.getMinutes(); var todaySecond = today.getSeconds(); /* Date.UTC() -- 返回date物件距世界標準時間(UTC)1970年1月1日午夜之間的毫秒數(時間戳) year - 作為date物件的年份,為4位年份值 month - 0-11之間的整數,做為date物件的月份 day - 1-31之間的整數,做為date物件的天數 hours - 0(午夜24點)-23之間的整數,做為date物件的小時數 minutes - 0-59之間的整數,做為date物件的分鐘數 seconds - 0-59之間的整數,做為date物件的秒數 microseconds - 0-999之間的整數,做為date物件的毫秒數 */
var t1 = Date.UTC(2016,12,01,00,00,00); //北京時間2016-12-1 00:00: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>