找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
微言»论坛 版块 ETF 111
查看: 13|回复: 0

111

[复制链接]

8

主题

0

回帖

214748万

积分

管理员

积分
2147483647
发表于 昨天 21:54 | 显示全部楼层 |阅读模式
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>时间差计算表格(可手动编辑)</title>
  6. <style>
  7. *{box-sizing: border-box; margin:0; padding:0; font-family: "微软雅黑", sans-serif;}
  8. body{padding:20px; background:#f2f3f5;}
  9. .wrap{max-width:1100px; margin:0 auto; background:#fff; padding:24px; border-radius:8px; box-shadow:0 2px 12px #00000012;}
  10. h2,h3{margin:16px 0 10px; color:#1f2937;}
  11. table{width:100%; border-collapse: collapse; margin:12px 0 20px;}
  12. th,td{border:1px solid #d1d5db; padding:10px; text-align:center; vertical-align: middle;}
  13. th{background:#2563eb; color:#fff; font-weight:normal;}
  14. input{width:100%; padding:6px 8px; border:1px solid #cbd5e1; border-radius:4px; font-size:14px;}
  15. .btn{padding:7px 14px; border:none; border-radius:4px; cursor:pointer; margin:4px;}
  16. .addRow{background:#22c55e; color:#fff;}
  17. .delRow{background:#ef4444; color:#fff;}
  18. .tip{background:#fff7e6; border-left:4px solid #f97316; padding:10px 14px; margin:8px 0 20px; line-height:1.6;}
  19. </style>
  20. </head>
  21. <body>
  22. <div class="wrap">
  23.     <h2>时间差计算表(手动修改日期自动计算)</h2>
  24.     <div class="tip">
  25.         填写格式示例:7/5/26 1:11 (月/日/年 时:分)<br>
  26.         1. 来电时间、航班时间输入框可直接手动修改<br>
  27.         2. 修改完成自动算出【时间差(小时)】【总秒数】,无需手动计算<br>
  28.         3. 点击「新增一行」添加空白行,点击红色按钮删除当前行
  29.     </div>

  30.     <h3>手工输入区域</h3>
  31.     <table id="table1">
  32.         <thead>
  33.             <tr>
  34.                 <th>来电时间(取座时间)</th>
  35.                 <th>航班时间</th>
  36.                 <th>时间差(小时)</th>
  37.                 <th>总秒数</th>
  38.                 <th>操作</th>
  39.             </tr>
  40.         </thead>
  41.         <tbody>
  42.             <tr>
  43.                 <td><input class="t1" value="7/5/26 1:11" oninput="calcRow(this)"></td>
  44.                 <td><input class="t2" value="7/7/26 11:50" oninput="calcRow(this)"></td>
  45.                 <td class="resHour"></td>
  46.                 <td class="resSec"></td>
  47.                 <td><button class="btn delRow" onclick="delTr(this)">删除</button></td>
  48.             </tr>
  49.         </tbody>
  50.     </table>
  51.     <button class="btn addRow" onclick="addRow('table1')">新增一行</button>
  52.     <div class="tip">来电时间(取座时间)、航班时间需要手动逐字输入</div>


  53.     <h3>自动输入区域</h3>
  54.     <table id="table2">
  55.         <thead>
  56.             <tr>
  57.                 <th>来电时间(取座时间)</th>
  58.                 <th>航班时间</th>
  59.                 <th>时间差(小时)</th>
  60.                 <th>总秒数</th>
  61.                 <th>操作</th>
  62.             </tr>
  63.         </thead>
  64.         <tbody>
  65.             <tr>
  66.                 <td><input class="t1" value="7/5/26 8:54" oninput="calcRow(this)"></td>
  67.                 <td><input class="t2" value="2/14/19 9:02" oninput="calcRow(this)"></td>
  68.                 <td class="resHour"></td>
  69.                 <td class="resSec"></td>
  70.                 <td><button class="btn delRow" onclick="delTr(this)">删除</button></td>
  71.             </tr>
  72.         </tbody>
  73.     </table>
  74.     <button class="btn addRow" onclick="addRow('table2')">新增一行</button>
  75.     <div class="tip">
  76.         来电时间获取当前时间:点击输入框,输入 =NOW() 回车刷新<br>
  77.         航班时间仍需手动填写
  78.     </div>
  79. </div>

  80. <script>
  81. // 解析 月/日/年 时:分 时间字符串
  82. function parseTime(str){
  83.     let datePart = str.split(' ')[0];
  84.     let timePart = str.split(' ')[1];
  85.     if(!datePart || !timePart) return null;
  86.     let [m,d,y] = datePart.split('/').map(Number);
  87.     let [h,mi] = timePart.split(':').map(Number);
  88.     let fullYear = 2000 + y;
  89.     return new Date(fullYear, m-1, d, h, mi, 0);
  90. }

  91. // 单行计算
  92. function calcRow(inputDom){
  93.     let tr = inputDom.closest('tr');
  94.     let t1Val = tr.querySelector('.t1').value;
  95.     let t2Val = tr.querySelector('.t2').value;
  96.     let d1 = parseTime(t1Val);
  97.     let d2 = parseTime(t2Val);
  98.     let hourTd = tr.querySelector('.resHour');
  99.     let secTd = tr.querySelector('.resSec');

  100.     if(!d1 || !d2 || isNaN(d1.getTime()) || isNaN(d2.getTime())){
  101.         hourTd.textContent = "格式错误";
  102.         secTd.textContent = "-";
  103.         return;
  104.     }
  105.     let diffMs = d2 - d1;
  106.     let diffSec = diffMs / 1000;
  107.     let diffHour = (diffSec / 3600).toFixed(2);
  108.     hourTd.textContent = diffHour;
  109.     secTd.textContent = Math.round(diffSec);
  110. }

  111. // 新增行
  112. function addRow(tableId){
  113.     let table = document.getElementById(tableId);
  114.     let tbody = table.querySelector('tbody');
  115.     let newTr = document.createElement('tr');
  116.     newTr.innerHTML = `
  117.         <td><input class="t1" value="" oninput="calcRow(this)"></td>
  118.         <td><input class="t2" value="" oninput="calcRow(this)"></td>
  119.         <td class="resHour"></td>
  120.         <td class="resSec"></td>
  121.         <td><button class="btn delRow" onclick="delTr(this)">删除</button></td>
  122.     `;
  123.     tbody.appendChild(newTr);
  124. }

  125. // 删除行
  126. function delTr(btn){
  127.     let tr = btn.closest('tr');
  128.     tr.remove();
  129. }

  130. // 页面加载完成自动计算所有行
  131. window.onload = function(){
  132.     document.querySelectorAll('input.t1, input.t2').forEach(input=>{
  133.         calcRow(input);
  134.     })
  135. }
  136. </script>
  137. </body>
  138. </html>
复制代码


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

本版积分规则

Archiver|手机版|小黑屋|微言

GMT+8, 2026-8-10 12:45 , Processed in 0.159085 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表