X
返回列表 发新帖

时间统计倒计时模块代码

[复制链接]
学徒 | 创客
逍遥亦自由 发表在  2026-4-9 14:38:28  | 显示全部楼层 | 阅读模式
资源
资源类型: 素材模板 » 网页模板
渠道授权: 研究学习 

马上注册,获取更多创业资源,认识更多朋友!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
这代码可以放在网页、APP上,如果能获取会员年龄,在底部还可以增加一项人生倒计时。
时间统计代码.jpg
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>混吃等死计时器</title>
  7.     <style>
  8.         * {
  9.             margin: 0;
  10.             padding: 0;
  11.             box-sizing: border-box;
  12.             font-family: "Microsoft Yahei", sans-serif;
  13.         }

  14.         body {
  15.             background-color: #f5f5f5;
  16.             padding: 20px;
  17.         }

  18.         .container {
  19.             max-width: 400px;
  20.             margin: 0 auto;
  21.             background: #fff;
  22.             padding: 25px;
  23.             border-radius: 12px;
  24.             box-shadow: 0 2px 10px rgba(0,0,0,0.08);
  25.         }

  26.         .title {
  27.             font-size: 22px;
  28.             font-weight: bold;
  29.             color: #333;
  30.             text-align: center;
  31.             margin-bottom: 25px;
  32.         }

  33.         .timer-item {
  34.             margin-bottom: 20px;
  35.         }

  36.         .item-label {
  37.             font-size: 15px;
  38.             color: #666;
  39.             margin-bottom: 8px;
  40.             display: flex;
  41.             justify-content: space-between;
  42.             align-items: center;
  43.         }

  44.         .progress-bar {
  45.             height: 12px;
  46.             background-color: #eee;
  47.             border-radius: 6px;
  48.             overflow: hidden;
  49.             position: relative;
  50.         }

  51.         .progress-fill {
  52.             height: 100%;
  53.             background: linear-gradient(to right, #86e586, #66cc66);
  54.             border-radius: 6px;
  55.             transition: width 0.5s ease;
  56.         }

  57.         .percent {
  58.             color: #ff6666;
  59.             font-weight: 500;
  60.         }
  61.     </style>
  62. </head>
  63. <body>
  64.     <div class="container">
  65.         <div class="title">混吃等死计时器</div>
  66.         
  67.         <!-- 今日进度 -->
  68.         <div class="timer-item">
  69.             <div class="item-label">
  70.                 <span id="today-text">今天已过去 0 小时</span>
  71.                 <span class="percent" id="today-percent">0%</span>
  72.             </div>
  73.             <div class="progress-bar">
  74.                 <div class="progress-fill" id="today-bar"></div>
  75.             </div>
  76.         </div>

  77.         <!-- 本周进度 -->
  78.         <div class="timer-item">
  79.             <div class="item-label">
  80.                 <span id="week-text">本周已过去 0 天</span>
  81.                 <span class="percent" id="week-percent">0%</span>
  82.             </div>
  83.             <div class="progress-bar">
  84.                 <div class="progress-fill" id="week-bar"></div>
  85.             </div>
  86.         </div>

  87.         <!-- 本月进度 -->
  88.         <div class="timer-item">
  89.             <div class="item-label">
  90.                 <span id="month-text">本月已过去 0 天</span>
  91.                 <span class="percent" id="month-percent">0%</span>
  92.             </div>
  93.             <div class="progress-bar">
  94.                 <div class="progress-fill" id="month-bar"></div>
  95.             </div>
  96.         </div>

  97.         <!-- 今年进度 -->
  98.         <div class="timer-item">
  99.             <div class="item-label">
  100.                 <span id="year-text">今年已过去 0 个月零 0 天</span>
  101.                 <span class="percent" id="year-percent">0%</span>
  102.             </div>
  103.             <div class="progress-bar">
  104.                 <div class="progress-fill" id="year-bar"></div>
  105.             </div>
  106.         </div>

  107.         <!-- 离春节倒计时 -->
  108.         <div class="timer-item">
  109.             <div class="item-label">
  110.                 <span id="spring-text">离春节还有 0 天 0 时</span>
  111.                 <span class="percent" id="spring-percent">0%</span>
  112.             </div>
  113.             <div class="progress-bar">
  114.                 <div class="progress-fill" id="spring-bar"></div>
  115.             </div>
  116.         </div>
  117.     </div>

  118.     <script>
  119.         // 获取当前时间
  120.         function getCurrentTime() {
  121.             return new Date();
  122.         }

  123.         // 计算今日进度(小时)
  124.         function calculateTodayProgress() {
  125.             const now = getCurrentTime();
  126.             const hours = now.getHours();
  127.             const minutes = now.getMinutes();
  128.             const totalHoursToday = hours + minutes / 60;
  129.             const percent = (totalHoursToday / 24) * 100;
  130.             
  131.             document.getElementById('today-text').textContent = `今天已过去 ${Math.floor(totalHoursToday)} 小时`;
  132.             document.getElementById('today-percent').textContent = `${Math.round(percent)}%`;
  133.             document.getElementById('today-bar').style.width = `${percent}%`;
  134.         }

  135.         // 计算本周进度(天)
  136.         function calculateWeekProgress() {
  137.             const now = getCurrentTime();
  138.             // 周日是0,周一到周六是1-6,转换为周一为第1天
  139.             let day = now.getDay();
  140.             day = day === 0 ? 7 : day; // 周日算第7天
  141.             const percent = (day / 7) * 100;
  142.             
  143.             document.getElementById('week-text').textContent = `本周已过去 ${day} 天`;
  144.             document.getElementById('week-percent').textContent = `${Math.round(percent)}%`;
  145.             document.getElementById('week-bar').style.width = `${percent}%`;
  146.         }

  147.         // 计算本月进度(天)
  148.         function calculateMonthProgress() {
  149.             const now = getCurrentTime();
  150.             const day = now.getDate();
  151.             const month = now.getMonth();
  152.             const year = now.getFullYear();
  153.             // 获取当月总天数
  154.             const totalDays = new Date(year, month + 1, 0).getDate();
  155.             const percent = (day / totalDays) * 100;
  156.             
  157.             document.getElementById('month-text').textContent = `本月已过去 ${day} 天`;
  158.             document.getElementById('month-percent').textContent = `${Math.round(percent)}%`;
  159.             document.getElementById('month-bar').style.width = `${percent}%`;
  160.         }

  161.         // 计算今年进度(月+天)
  162.         function calculateYearProgress() {
  163.             const now = getCurrentTime();
  164.             const year = now.getFullYear();
  165.             const month = now.getMonth(); // 0-11
  166.             const day = now.getDate();
  167.             
  168.             // 计算今年已过的总天数
  169.             let passedDays = 0;
  170.             for (let i = 0; i < month; i++) {
  171.                 passedDays += new Date(year, i + 1, 0).getDate();
  172.             }
  173.             passedDays += day;
  174.             
  175.             // 计算今年总天数(判断闰年)
  176.             const isLeapYear = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
  177.             const totalDays = isLeapYear ? 366 : 365;
  178.             const percent = (passedDays / totalDays) * 100;
  179.             
  180.             // 格式化显示:X个月零Y天
  181.             document.getElementById('year-text').textContent = `今年已过去 ${month} 个月零 ${day} 天`;
  182.             document.getElementById('year-percent').textContent = `${Math.round(percent)}%`;
  183.             document.getElementById('year-bar').style.width = `${percent}%`;
  184.         }

  185.         // 优化:获取下一个春节的日期(2025-2030年春节日期,可扩展)
  186.         function getNextSpringFestivalDate(currentYear) {
  187.             // 春节日期对照表(月/日,月份从0开始)
  188.             const springFestivalDates = {
  189.                 2025: [0, 29],   // 2025年1月29日
  190.                 2026: [0, 17],   // 2026年1月17日
  191.                 2027: [1, 6],    // 2027年2月6日
  192.                 2028: [1, 26],   // 2028年2月26日
  193.                 2029: [1, 13],   // 2029年2月13日
  194.                 2030: [1, 3],    // 2030年2月3日
  195.             };
  196.             
  197.             // 获取当前年份的春节日期
  198.             let targetYear = currentYear;
  199.             let springMonth = springFestivalDates[targetYear][0];
  200.             let springDay = springFestivalDates[targetYear][1];
  201.             let springDate = new Date(targetYear, springMonth, springDay);
  202.             
  203.             // 如果当前年份的春节已过,取明年的
  204.             const now = getCurrentTime();
  205.             if (now > springDate) {
  206.                 targetYear += 1;
  207.                 springMonth = springFestivalDates[targetYear][0];
  208.                 springDay = springFestivalDates[targetYear][1];
  209.                 springDate = new Date(targetYear, springMonth, springDay);
  210.             }
  211.             
  212.             return {
  213.                 date: springDate,
  214.                 year: targetYear
  215.             };
  216.         }

  217.         // 优化:计算距离下一年春节的倒计时
  218.         function calculateSpringFestival() {
  219.             const now = getCurrentTime();
  220.             const currentYear = now.getFullYear();
  221.             const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
  222.             
  223.             // 获取下一个春节的日期
  224.             const nextSpring = getNextSpringFestivalDate(currentYear);
  225.             const springDate = nextSpring.date;
  226.             const springYear = nextSpring.year;
  227.             
  228.             // 计算剩余时间
  229.             const diffMs = springDate - now;
  230.             const days = Math.floor(diffMs / oneDay);
  231.             const hours = Math.floor((diffMs % oneDay) / (60 * 60 * 1000));
  232.             
  233.             // 计算进度百分比:从本年1月1日到现在的天数 / 从本年1月1日到春节的总天数
  234.             const startOfYear = new Date(currentYear, 0, 1); // 本年1月1日
  235.             const totalDaysToSpring = Math.floor((springDate - startOfYear) / oneDay); // 年初到春节总天数
  236.             const passedDays = Math.floor((now - startOfYear) / oneDay); // 年初到现在已过天数
  237.             const percent = totalDaysToSpring > 0 ? (passedDays / totalDaysToSpring) * 100 : 0;
  238.             
  239.             // 更新界面显示
  240.             document.getElementById('spring-text').textContent = `离${springYear}年春节还有 ${days} 天 ${hours} 时`;
  241.             document.getElementById('spring-percent').textContent = `${Math.round(percent)}%`;
  242.             document.getElementById('spring-bar').style.width = `${percent}%`;
  243.         }

  244.         // 初始化所有进度
  245.         function initAllProgress() {
  246.             calculateTodayProgress();
  247.             calculateWeekProgress();
  248.             calculateMonthProgress();
  249.             calculateYearProgress();
  250.             calculateSpringFestival();
  251.         }

  252.         // 页面加载时初始化,每分钟更新一次(提升倒计时精度)
  253.         window.onload = function() {
  254.             initAllProgress();
  255.             setInterval(initAllProgress, 60 * 1000);
  256.         };
  257.     </script>
  258. </body>
  259. </html>
复制代码


发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们
关于我们
友情链接
联系我们
帮助中心
网友中心
购买须知
支付方式
服务支持
解决方案
售后服务
定制流程
关注我们
官方微博
官方空间
官方微信
快速回复 返回顶部 返回列表