网站首页 >> 教程 >> 正文
标题

本站已稳定运行 X天 X 小时X分X秒完整代码

逍遥   12月23日 08:57   10℃   0
内容

本站已稳定运行 X天 X 小时X分X秒完整代码,image.png,本站已稳定运行 X天 X 小时X分X秒,第1张

本站美化详情代码

<!DOCTYPE html> <html> <head>     <meta charset="UTF-8">     <title>网站运行时长显示</title>     <style>         /* 可选:简单美化显示样式 */         .run-time {             color: #007bff;             font-weight: bold;             text-decoration: none;         }     </style> </head> <body>     <!-- 显示运行时长的容器 -->     <a href="#" id="siteRuntime">本站已稳定运行 0 天 0 小时 0 分 0 秒</a>     <script>         // 核心功能:计算并实时更新网站运行时长         (function() {             // ========== 关键配置:替换为你的网站上线时间 ==========             // 格式:年, 月(0-11), 日, 时, 分, 秒             const launchDate = new Date(2018, 0, 1, 0, 0, 0); // 示例:2018年1月1日 0点0分0秒             // =====================================================             // 补零函数:确保分/秒等小于10时显示0开头(如05分,而非5分)             function padZero(num) {                 return num < 10 ? '0' + num : num;             }             // 计算时间差并更新显示             function updateRuntime() {                 // 获取当前时间                 const now = new Date();                 // 计算时间差(毫秒)                 const diffMs = now - launchDate;                 // 转换为总秒数                 const totalSeconds = Math.floor(diffMs / 1000);                 // 分解为天	、时、分、秒                 const days = Math.floor(totalSeconds / (24 * 60 * 60));                 const hours = Math.floor((totalSeconds % (24 * 60 * 60)) / (60 * 60));                 const minutes = Math.floor((totalSeconds % (60 * 60)) / 60);                 const seconds = Math.floor(totalSeconds % 60);                 // 更新DOM显示                 const runtimeEl = document.getElementById('siteRuntime');                 runtimeEl.textContent = `本站已稳定运行 ${days} 天 ${padZero(hours)} 小时 ${padZero(minutes)} 分 ${padZero(seconds)} 秒`;             }             // 初始化执行一次             updateRuntime();             // 每秒更新一次(1000毫秒)             setInterval(updateRuntime, 1000);         })();     </script> </body> </html>


控制面板
您好,欢迎到访网站!
  查看权限
网站分类
随机标签