点滴珍藏|快乐分享
当前位置: 建站编程 > 用 JavaScript 给网站添加运行天数

用 JavaScript 给网站添加运行天数

2017-03-16 分类:建站编程 作者:西狂 阅读(8)

只需要将一下的代码,放在想要显示的地方(footer、sidebar 等等),修改好其中自定义的网站建成时间和相关文字,就可以直接运行了。当然你可以修改 sitetime 的 CSS ,让它更有效果。

废话不说,直接上代码:

<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();

        var todayDate = today.getDate();

        var todayHour = today.getHours();

        var todayMinute = today.getMinutes();

        var todaySecond = today.getSeconds();



        /* 修改下面的t1数值为网站建成时间(年,月,日,时,分,秒)*/

        var t1 = Date.UTC(2016,7,6,0,0,0);

        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) 打赏

支付宝
微信
0

支付宝
微信
标签:

上一篇:

下一篇:

你可能感兴趣

共有 0 条评论 - 用 JavaScript 给网站添加运行天数

博客简介

欢迎来到西狂部落阁

精彩评论