JavaScript显示网站运行时间

个人还是比较喜欢这东西的,所以看了下代码,有需要的朋友直接修改下日期然后Copy过去使用即可。

方法一:只显示运行天数

这个简单很多,只显示运行天数,代码如下:

本站已安全运行了<b><strong><script language = "JavaScript"type = "text/javascript" >var urodz = new Date("12/19/2014");var now = new Date();var ile = now.getTime() - urodz.getTime();var dni = Math.floor(ile / (1000 * 60 * 60 * 24));document.write( + dni)</script></strong></b>天

其中12是月份,19是日期,2014是年数,根据自己情况修改一下即可,然后把代码复制到你想要显示的地方。

方法二:显示XX年XX天XX时XX秒,秒数还是一秒一秒的走动

这个相对上面那个来说代码比较多,不过也是Copy过去修改个日期即可,看代码:

<script>
function secondToDate(second) {
if (!second) {
return 0;
}
var time = new Array(0, 0, 0, 0, 0);
if (second >= 365 * 24 * 3600) {
time[0] = parseInt(second / (365 * 24 * 3600));
second %= 365 * 24 * 3600;
}
if (second >= 24 * 3600) {
time[1] = parseInt(second / (24 * 3600));
second %= 24 * 3600;
}
if (second >= 3600) {
time[2] = parseInt(second / 3600);
second %= 3600;
}
if (second >= 60) {
time[3] = parseInt(second / 60);
second %= 60;
}
if (second > 0) {
time[4] = second;
}
return time;
}</script><script type="text/javascript" language="javascript">
function setTime() {
var create_time = Math.round(new Date(Date.UTC(2015, 07, 09, 6, 6, 6)).getTime() / 1000);
var timestamp = Math.round((new Date().getTime() + 8 * 60 * 60 * 1000) / 1000);
currentTime = secondToDate((timestamp - create_time));
currentTimeHtml = currentTime[0] + '年' + currentTime[1] + '天'
+ currentTime[2] + '时' + currentTime[3] + '分' + currentTime[4]
+ '秒';
document.getElementById("htmer_time").innerHTML = currentTimeHtml;
} setInterval(setTime, 1000);</script>

把上面代码复制到网页文件中,然后在你想显示的地方贴上这么一句代码即可:

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

具体CSS样式自行修改,上面代码中2015,07,09,6,6,6分别对应年、月、日、时、分、秒,根据自己实际修改即可。

原文地址:http://citywo.com/index.php/post/465.html

This entry was posted in Technology and tagged , , . Bookmark the permalink. 619 views

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>