分享博客的动态运行天数代码

今天偶然看到别人博客中可以显示博客的运行时间

于是自己也想做一个,就找了一下方法,最后找到一个比较方便的,就是贴代码,一开始还担心没有用,结果很完美

代码如下:

 1 <script>
 2     function secondToDate(second) {
 3         if (!second) {
 4             return 0;
 5         }
 6         var time = new Array(0, 0, 0, 0, 0);
 7         if (second >= 365 * 24 * 3600) {
 8             time[0] = parseInt(second / (365 * 24 * 3600));
 9             second %= 365 * 24 * 3600;
10         }
11         if (second >= 24 * 3600) {
12             time[1] = parseInt(second / (24 * 3600));
13             second %= 24 * 3600;
14         }
15         if (second >= 3600) {
16             time[2] = parseInt(second / 3600);
17             second %= 3600;
18         }
19         if (second >= 60) {
20             time[3] = parseInt(second / 60);
21             second %= 60;
22         }
23         if (second > 0) {
24             time[4] = second;
25         }
26         return time;
27     }
28 </script>
29 <script type="text/javascript" language="javascript">
30     function setTime() {
31         // 博客创建时间秒数,时间格式中,月比较特殊,是从0开始的,所以想要显示5月,得写4才行,如下
32         var create_time = Math.round(new Date(Date.UTC(2013, 4, 22, 0, 0, 0))
33                 .getTime() / 1000);
34         // 当前时间秒数,增加时区的差异
35         var timestamp = Math.round((new Date().getTime() + 8 * 60 * 60 * 1000) / 1000);
36         currentTime = secondToDate((timestamp - create_time));
37         currentTimeHtml = currentTime[0] + '年' + currentTime[1] + '天'
38                 + currentTime[2] + '时' + currentTime[3] + '分' + currentTime[4]
39                 + '秒';
40         document.getElementById("htmer_time").innerHTML = currentTimeHtml;
41     }
42     setInterval(setTime, 1000);
43 </script>

将这段代码放在页脚代码中

1 网站运行:<span id="htmer_time" style="color: red;"></span>

然后将上面这行代码插入统计代码当中或网站合适的位置即可

猜你喜欢

转载自www.cnblogs.com/Deast/p/10179070.html