用js实现网站运行时间的统计

代码1
<span id="span_dt_dt"></span>
<script type="text/javascript">
    function show_date_time() {
        window.setTimeout("show_date_time()", 1000);
        BirthDay = new Date("2018/02/16 00:00:00");
        today = new Date();
        timeold = (today.getTime() - BirthDay.getTime());
        sectimeold = timeold / 1000;
        secondsold = Math.floor(sectimeold);
        msPerDay = 24 * 60 * 60 * 1000;
        e_daysold = timeold / msPerDay;
        daysold = Math.floor(e_daysold);
        e_hrsold = (e_daysold - daysold) * 24;
        hrsold = Math.floor(e_hrsold);
        e_minsold = (e_hrsold - hrsold) * 60;
        minsold = Math.floor((e_hrsold - hrsold) * 60);
        seconds = Math.floor((e_minsold - minsold) * 60);
        span_dt_dt.innerHTML = "此破站已苟活:" + daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒";
    }
    show_date_time();
</script>
代码2
<span id="sitetime"></span>
<script type="text/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(2018,02,16,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>
代码3
<span>博客已萌萌哒运行</span><span id=span_dt_dt></span>
<script type="text/javascript">
    function show_date_time() {
        window.setTimeout("show_date_time()", 1e3);
        var BirthDay = new Date("2018/02/16 00:00:00"), today = new Date,
            timeold = today.getTime() - BirthDay.getTime(), msPerDay = 864e5, e_daysold = timeold / msPerDay,
            daysold = Math.floor(e_daysold), e_hrsold = 24 * (e_daysold - daysold), hrsold = Math.floor(e_hrsold),
            e_minsold = 60 * (e_hrsold - hrsold), minsold = Math.floor(60 * (e_hrsold - hrsold)),
            seconds = Math.floor(60 * (e_minsold - minsold));
        span_dt_dt.innerHTML = daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒";
    }
    show_date_time();
</script>
Last modification:April 26th, 2020 at 03:19 pm