0%

给网站加运行时间

给网站加运行时间

使用到的知识点

1
2
3
4
JS:
Date 对象
window.setTimeout
Math.floor()

复制如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<span>本站已经安全运行</span>
<span id="span_day"></span>

<script>
function show_time(){
window.setTimeout("show_time()", 1000);
var birthday = new Date("7/13/2019");<!--修改为建站的时间-->
var today = new Date();

var oldTime = today.getTime() - birthday.getTime();
var perDay = 1000*60*60*24;
var e_dayLeft = oldTime/perDay;
var dayLeft = Math.floor(e_dayLeft);
var e_hrLeft = (e_dayLeft - dayLeft)*24;
var hrLeft = Math.floor(e_hrLeft);
var e_minLeft = (e_hrLeft - hrLeft)*60;
var minLeft = Math.floor(e_minLeft);
var secLeft = Math.floor((e_minLeft - minLeft)*60);
var span_day.innerHTML=dayLeft+"天"+hrLeft+"小时"+minLeft+"分"+secLeft+"秒";
}

show_time();
</script>

效果可以参考我的主站 inkWall;