设置网站运行时间
网站运行时间计算一
代码语言:javascript
复制
/**
* 底部运行时间计算(记得设置html<span id="old-now"></span>)
*/
function showDatetime() {
window.setTimeout("showDatetime()", 1000);
let BirthDay = new Date("08/01/2019 00:00:00");
let today = new Date();
let timeold = (today.getTime() - BirthDay.getTime());
let sectimeold = timeold / 1000;
secondsold = Math.floor(sectimeold);
let msPerDay = 24 * 60 * 60 * 1000;
let e_daysold = timeold / msPerDay;
let daysold = Math.floor(e_daysold);
let e_hrsold = (e_daysold - daysold) * 24;
let hrsold = setZero(Math.floor(e_hrsold));
let e_minsold = (e_hrsold - hrsold) * 60;
let minsold = setZero(Math.floor((e_hrsold - hrsold) * 60));
let seconds = setZero(Math.floor((e_minsold - minsold) * 60));
document.querySelector('#old-now').innerHTML = " 已运行 " + daysold + " 天 " + hrsold + " 小时 " + minsold + " 分 <strong style='color:#A52A2A'>" + seconds + "</strong> 秒";
}
/**
- 归零
@param {Integer} nothing。
*/
function setZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
网站运行时间计算二
代码语言:javascript
复制
function createTime() {
var now = new Date();
var run = new Date("05/28/2018 20:01:01");
//总秒数
var runTime = (now - run) / 1000,
days = Math.floor(runTime / 60 / 60 / 24),
hours = Math.floor(runTime / 60 / 60 - (24 * days)),
minutes = Math.floor(runTime / 60 - (24 * 60 * days) - (60 * hours)),
seconds = Math.floor((now - run) / 1000 - (24 * 60 * 60 * days) - (60 * 60 * hours) - (60 * minutes));
//前置零
if (String(hours).length === 1) {
hours = "0" + hours;
}
if (String(minutes).length === 1) {
minutes = "0" + minutes;
}
if (String(seconds).length === 1) {
seconds = "0" + seconds;
}
/*document.querySelector(".run-times").innerHTML = days + " 天 " + hours
+ " 时 " + minutes + " 分 " + seconds + " 秒";
*/
document.querySelector(".run-times").innerHTML = "RunTime: " + days + "," + hours
+ ":" + minutes + ":" + seconds + "";
}
//setInterval("createTime()", 500);
if (!document.hidden) {
var siteTime = setInterval("createTime()", 500);
} else {
clearInterval(siteTime);
}
/**
HTML写法
<span class="run-times" title="网站运行时间">载入中...</span>
<p class="run-times" title="主页运行时间">RunTime Loading...</p>
*/