话不多说,代码摆上来壮威!
本站使用代码:
- <html>
- <center>
- 网站已运行: <span id="run_time" style="color:red" ></span>
- </center>
- </html>
- <script>
- function runTime(){
- var d = new Date(),str = '';
- BirthDay=new Date("march 08,2017");
- today=new Date();
- timeold=(today.getTime()-BirthDay.getTime());
- sectimeold=timeold/1000
- secondsold=Math.floor(sectimeold);
- msPerDay=24*60*60*1000
- msPerYear=365*24*60*60*1000
- e_daysold=timeold/msPerDay
- e_yearsold=timeold/msPerYear
- daysold=Math.floor(e_daysold);
- yearsold=Math.floor(e_yearsold);
- str = yearsold+"年"+daysold+"天";
- str += d.getHours()+'时';
- str += d.getMinutes()+'分';
- str+= d.getSeconds()+'秒';
- return str;
- }
- setInterval(function(){$('#run_time').html(runTime())},1000);
- </script>
第二种
<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(2016,12,01,00,00,00); //北京时间 2016-12-1 00:00:00 var t2 = Date.UTC(todayYear,todayMonth,todayDate,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>