方法一:通过JS计算时间差 获取网站运行时间
来源:https://yantuz.cn/322.html
实现效果
由于文章插入JS不安全,所以iframe引用一下:
源码示例:
代码已托管于Coidng.net地址:yhf7952/JS_Website_Runtime
GitHub地址:yhf7952/JS_Website_Runtime
演示地址:https://yhf7952.coding.me/JS_Website_Runtime/
iframe引用方法:
<iframe src="https://yhf7952.coding.me/JS_Website_Runtime/" width="400" height="40" frameborder="0" scrolling="no">
您的浏览器不支持 iframe 标签
</iframe>
JS代码:
runTime方法,可接收6个参数,分别是年月日时分秒,系统将自动生成入参日期至今的时间,并每秒更新。
function runTime(y=1970,m=0,d=1,h=0,mm=0,s=0){
window.setTimeout(function(){runTime(y,m,d,h,mm,s)}, 1000);
var diff = newDate() - newDate(y,m-1,d,h,mm,s);
var days = Math.floor(diff/(24*3600*1000));
var hours = Math.floor((diff%(24*3600*1000))/(3600*1000));
var minutes = Math.floor(diff%(3600*1000)/(60*1000));
var seconds = Math.floor(diff%(60*1000)/1000);
if(document.getElementById("siteTime"))
document.getElementById("siteTime").innerHTML=days+"天"+hours+"小时"+minutes+"分"+seconds+"秒";
else
document.write('<span id="siteTime">'+days+"天"+hours+"小时"+minutes+"分"+seconds+"秒</span>");
}
调用方法,根据实际情况更改参数为网站上线时间即可
runTime(2017,12,20,8,05,20);
实现原理
- 根据参数输入的起始时间,自动计算起始时间至今的天、时、分、秒
- 输出span显示计算结果,如已存在span则更新结果
- 每秒钟运行程序,计算最新结果以实现动态显示
在HTML中调用
网站已萌萌哒运行了<script>runTime(2017,12,20,8,05,20);</script>
源码内容:
index.html 文件内容
<html>
<body>
<script type="text/javascript">
function runTime(y=1970,m=0,d=1,h=0,mm=0,s=0){
window.setTimeout(function(){runTime(y,m,d,h,mm,s)}, 1000);
var diff = new Date() - new Date(y,m-1,d,h,mm,s);
//document.write(diff);
var days = Math.floor(diff/(24*3600*1000));
var hours = Math.floor((diff%(24*3600*1000))/(3600*1000));
var minutes = Math.floor(diff%(3600*1000)/(60*1000));
var seconds = Math.floor(diff%(60*1000)/1000);
if(document.getElementById("siteTime"))
document.getElementById("siteTime").innerHTML=days+"天"+hours+"小时"+minutes+"分"+seconds+"秒";
else
document.write('<span id="siteTime">'+days+"天"+hours+"小时"+minutes+"分"+seconds+"秒</span>");
}
</script>
网站已萌萌哒运行了<script>runTime(2017,12,20,8,05,47);</script>
</body>
</html>
方法二:
<script type="text/javascript">function show_runtime(){window.setTimeout("show_runtime()",1000);X=new
Date("05/23/2019 12:22:00");
Y=new Date();T=(Y.getTime()-X.getTime());M=24*60*60*1000;
a=T/M;A=Math.floor(a);b=(a-A)*24;B=Math.floor(b);c=(b-B)*60;C=Math.floor((b-B)*60);D=Math.floor((c-C)*60);
runtime_span.innerHTML="已运行网站: "+A+"天"+B+"小时"+C+"分"+D+"秒"}show_runtime();</script>
方法三:
js统计网站运行时长
<span id="times_bbwidc" style="color:#009900"></span>
<script language="javascript">
<!--
function show_date_time(){
window.setTimeout("show_date_time()", 1000);
BirthDay=new Date("09,25,2018,11:05:00"); //北京时间 月,日,年 时:分:秒
today=new Date();
timeold=(today.getTime()-BirthDay.getTime());
sectimeold=timeold/1000
secondsold=Math.floor(sectimeold);
msPerDay=24*60*60*1000
e_daysold=timeold/msPerDay
daysold=Math.floor(e_daysold);
e_hrsold=(daysold-e_daysold)*-24;
hrsold=Math.floor(e_hrsold);
e_minsold=(hrsold-e_hrsold)*-60;
minsold=Math.floor((hrsold-e_hrsold)*-60);
seconds=Math.floor((minsold-e_minsold)*-60);
times_bbwidc.innerHTML="本站已稳定运行:"+daysold+" 天 "+hrsold+" 小时 "+minsold+" 分 "+seconds+" 秒 " ;
}
show_date_time();
//-->
</script>
显示XX年XX天XX时XX秒
网站<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()+1;
var todayDate = today.getDate();
var todayHour = today.getHours();
var todayMinute = today.getMinutes();
var todaySecond = today.getSeconds();
/* Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
year - 作为date对象的年份,为4位年份值
month - 0-11之间的整数,做为date对象的月份
day - 1-31之间的整数,做为date对象的天数
hours - 0(午夜24点)-23之间的整数,做为date对象的小时数
minutes - 0-59之间的整数,做为date对象的分钟数
seconds - 0-59之间的整数,做为date对象的秒数
microseconds - 0-999之间的整数,做为date对象的毫秒数 */
var t1 = Date.UTC(2018,08,24,18,25,00); //北京时间2018-08-24 18:25:00
var t2 = Date.UTC(todayYear,todayMonth,todayDate-1,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>
演示:网站已运行2 年 91 天 13 小时 24 分钟 31 秒
注释:代码中的 var t1 = Date.UTC(2018,08,24,18,25,00); 是网站上线时间(自己定义)//北京时间2018-08-24 18:25:00
第二种:只显示运行天数
网站已运行<script language="javascript">
var now=new Date();var spday=new Date(2016,09,22);a=(now.getTime()-spday.getTime())/(24*60*60*1000);a=Math.ceil(a); document.write
("<b>"+a+"</b>");</script>天
演示: 网站已运行681天
注释: 代码中的 Date(2016,09,22) 是网站上线时间(自己定义),2016,09,22 为 2016年8月22日(月份0-11之间的整)
方法三
需要调用的HTML页面 </body> 前增加下方代码
<script type="text/javascript">
function show_date_time() {
if ($("#span_dt_dt").length > 0) {
window.setTimeout("show_date_time()", 1000);
BirthDay = new Date("2018/8/1");
today = new Date();
timeold = (today.getTime() - BirthDay.getTime());
sectimeold = timeold / 1000;
secondsold = Math.floor(sectimeold);
msPerDay = 24 * 60 * 60 * 1000;
e_daysold = timeold / msPerDay;
daysold = Math.floor(e_daysold);
e_hrsold = (e_daysold - daysold) * 24;
hrsold = Math.floor(e_hrsold);
e_minsold = (e_hrsold - hrsold) * 60;
minsold = Math.floor((e_hrsold - hrsold) * 60);
seconds = Math.floor((e_minsold - minsold) * 60);
span_dt_dt.innerHTML = daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒";
};
}
show_date_time();
</script>
在当页需要调用的网页处写入
<p>本站运行:<span id="span_dt_dt"></span></p>
即可
这是评论测试
飞歌导航东方红烦得很
这是新的一条评论
未来一年,中国的移动支付市场会是什么样?