javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > JavaScript时间戳转换工具

基于JavaScript编写一个时间戳转换工具

作者:李少兄

在现代 Web 开发中,时间戳是系统间传递时间信息的标准格式,我们常常需要将人类可读的时间转换为 Unix 时间戳或 JavaScript 时间戳,下面我们就来看看如何编写一个时间戳转换工具吧

一、起因:为什么需要这个工具

在现代 Web 开发中,时间戳(Timestamp)是系统间传递时间信息的标准格式。无论是调用后端 API、记录日志、还是处理缓存策略,我们常常需要将人类可读的时间(如 2025-01-01 12:30:45)转换为 Unix 时间戳(秒)或 JavaScript 时间戳(毫秒)。

然而,在以下场景中,这一过程变得繁琐:

因此,我决定开发一个纯前端、零依赖、开箱即用的时间戳转换工具。它无需安装、不依赖任何库、可在任意浏览器中运行,且兼顾专业性与易用性。

效果预览:

二、需求与设计原则

2.1 功能需求

双向转换

极致交互体验

用户体验

健壮性

2.2 设计原则

三、技术实现

3.1 界面结构

工具分为两个主要区域:

时间转时间戳

时间戳转时间

关键细节<input type="time"> 设置 step="1" 以支持秒级精度(默认只到分钟)。

3.2点击输入框即可弹出选择器

解决方案:使用.showPicker()API

现代浏览器(Chrome ≥96, Safari ≥16.4, Firefox ≥121)提供了 HTMLInputElement.showPicker() 方法,允许 JavaScript 主动触发原生选择器。

function enhancePicker(input) {
  input.addEventListener('click', () => {
    if (typeof input.showPicker === 'function') {
      try {
        input.showPicker(); // 主动唤出选择器
      } catch (e) {
        // 安全环境可能阻止,静默忽略
      }
    }
  });
}

// 应用到日期和时间输入框
enhancePicker(dateInput);
enhancePicker(timeInput);

3.3 手动输入与选择器的融合

无论用户是通过选择器选中日期,还是直接在输入框中键入 2025-06-15,都会触发 input 事件。我们只需在回调中读取 .value 并尝试解析即可。

智能解析策略

function parseUserInput(dateStr, timeStr) {
  const today = new Date();
  const fallbackDate = dateStr || today.toISOString().slice(0, 10); // YYYY-MM-DD
  const fallbackTime = timeStr || '00:00:00';
  let isoStr = `${fallbackDate}T${fallbackTime}`;
  let date = new Date(isoStr);
  
  if (isNaN(date.getTime()) && dateStr) {
    // 若时间部分非法,尝试仅用日期
    date = new Date(`${dateStr}T00:00:00`);
  }
  
  return isNaN(date.getTime()) ? null : date;
}

3.4 时间戳解析逻辑

对于时间戳输入,需处理以下情况:

输入示例处理方式
1704067200判定为秒 → ×1000 → 毫秒
1704067200000判定为毫秒 → 直接使用
abc123def提取数字 → 123 → 判定为秒
-123允许(历史时间),但需在有效范围内

有效范围校验:JavaScript 的 Date 对象有效范围为 ±100,000,000 天(约 ±273,972 年),对应毫秒值约为 ±8.64e15。我们设置安全边界:

if (ms < -8640000000000000 || ms > 8640000000000000) {
  // 超出范围
}

3.5 用户反馈机制

四、完整源码

以下为可直接保存为 .html 文件并在浏览器中打开的代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>时间戳转换工具</title>
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    body {
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'PingFang SC', sans-serif;
      background-color: #f8fafc;
      color: #1e293b;
      line-height: 1.5;
      padding: 24px;
    }
    .container {
      max-width: 680px;
      margin: 0 auto;
    }
    header {
      text-align: center;
      margin-bottom: 32px;
    }
    h1 {
      font-size: 26px;
      font-weight: 700;
      color: #0f172a;
    }
    .card {
      background: white;
      border-radius: 12px;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.04);
      padding: 24px;
      margin-bottom: 24px;
    }
    .section-title {
      font-size: 18px;
      font-weight: 600;
      margin-bottom: 20px;
      color: #334155;
    }
    .form-row {
      display: flex;
      gap: 16px;
      margin-bottom: 20px;
    }
    @media (max-width: 600px) {
      .form-row {
        flex-direction: column;
        gap: 12px;
      }
    }
    .field {
      flex: 1;
    }
    .field label {
      display: block;
      font-size: 14px;
      font-weight: 500;
      margin-bottom: 6px;
      color: #475569;
    }
    input[type="date"],
    input[type="time"],
    input[type="text"] {
      width: 100%;
      padding: 10px 12px;
      border: 1px solid #cbd5e1;
      border-radius: 8px;
      font-size: 16px;
      transition: border-color 0.2s;
    }
    input:focus {
      outline: none;
      border-color: #3b82f6;
      box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.15);
    }
    .unit-toggle {
      display: inline-flex;
      background: #f1f5f9;
      border-radius: 8px;
      padding: 4px;
      font-size: 14px;
    }
    .unit-option {
      padding: 6px 12px;
      border-radius: 6px;
      cursor: pointer;
      user-select: none;
      transition: all 0.15s;
    }
    .unit-option.active {
      background: white;
      color: #1d4ed8;
      font-weight: 600;
      box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    }

    .result-line {
      display: flex;
      gap: 12px;
      align-items: center;
      margin-top: 8px;
    }
    .result-line label {
      font-size: 14px;
      color: #64748b;
      font-weight: 500;
      min-width: 50px;
    }
    .result-line input[readonly] {
      flex: 1;
      background: #f8fafc;
      color: #1e293b;
      font-family: monospace;
      font-size: 15px;
      padding: 10px 12px;
      border: 1px solid #e2e8f0;
      border-radius: 8px;
    }
    .copy-btn {
      padding: 10px 16px;
      background: #e2e8f0;
      color: #475569;
      border: none;
      border-radius: 8px;
      font-size: 14px;
      cursor: pointer;
      transition: all 0.2s;
      min-width: 72px;
      white-space: nowrap;
    }
    .copy-btn:hover {
      background: #cbd5e1;
    }
    .copy-btn.copied {
      background: #10b981 !important;
      color: white !important;
    }

    .message {
      height: 20px;
      text-align: center;
      font-size: 14px;
      margin-top: 12px;
    }
    .message.error {
      color: #ef4444;
    }
  </style>
</head>
<body>
  <div class="container">
    <header>
      <h1>时间戳转换工具</h1>
    </header>

    <!-- 时间 → 时间戳 -->
    <div class="card">
      <div class="section-title">🕒 时间转时间戳</div>

      <div class="form-row">
        <div class="field">
          <label for="customDate">日期</label>
          <input type="date" id="customDate" />
        </div>
        <div class="field">
          <label for="customTime">时间</label>
          <input type="time" id="customTime" step="1" />
        </div>
      </div>

      <div class="field">
        <label>输出单位</label>
        <div class="unit-toggle" id="unitToggle">
          <div class="unit-option active" data-value="ms">毫秒(JavaScript)</div>
          <div class="unit-option" data-value="s">秒(Unix)</div>
        </div>
      </div>

      <div class="result-line">
        <label>结果</label>
        <input type="text" id="generatedOutput" readonly />
        <button class="copy-btn" id="copyGenerated">复制</button>
      </div>
      <div class="message" id="msgGen"></div>
    </div>

    <!-- 时间戳 → 时间 -->
    <div class="card">
      <div class="section-title">🔢 时间戳转时间</div>

      <div class="field">
        <label for="inputTimestamp">输入时间戳(支持秒或毫秒)</label>
        <input type="text" id="inputTimestamp" placeholder="例如:1664294400 或 1664294400000" />
      </div>

      <div class="result-line">
        <label>本地时间</label>
        <input type="text" id="readableTime" readonly />
        <button class="copy-btn" id="copyReadable">复制</button>
      </div>
      <div class="result-line">
        <label>毫秒</label>
        <input type="text" id="outputMs" readonly />
        <button class="copy-btn" id="copyParsedMs">复制</button>
      </div>
      <div class="result-line">
        <label>秒</label>
        <input type="text" id="outputSec" readonly />
        <button class="copy-btn" id="copyParsedSec">复制</button>
      </div>
      <div class="message" id="msgParse"></div>
    </div>
  </div>

  <script>
    // 初始化为当前时间
    const now = new Date();
    document.getElementById('customDate').valueAsDate = now;
    document.getElementById('customTime').value = now.toTimeString().slice(0, 8);

    const dateInput = document.getElementById('customDate');
    const timeInput = document.getElementById('customTime');
    const unitToggle = document.getElementById('unitToggle');
    const generatedOutput = document.getElementById('generatedOutput');
    const msgGen = document.getElementById('msgGen');

    // ✅ 关键增强:点击输入框任意位置,主动唤出选择器(现代浏览器)
    function enhancePicker(input) {
      // 点击输入框时尝试唤出原生选择器
      input.addEventListener('click', () => {
        if (typeof input.showPicker === 'function') {
          try {
            input.showPicker();
          } catch (e) {
            // 某些环境可能不允许(如安全限制),静默忽略
          }
        }
      });
    }

    // 应用增强
    enhancePicker(dateInput);
    enhancePicker(timeInput);

    function parseUserInput(dateStr, timeStr) {
      if (!dateStr && !timeStr) return null;

      const today = new Date();
      const fallbackDate = dateStr || today.toISOString().slice(0, 10);
      const fallbackTime = timeStr || '00:00:00';

      let isoStr = `${fallbackDate}T${fallbackTime}`;
      let date = new Date(isoStr);
      
      if (isNaN(date.getTime()) && dateStr) {
        date = new Date(`${dateStr}T00:00:00`);
      }

      return isNaN(date.getTime()) ? null : date;
    }

    function updateFromDateTime() {
      const dateStr = dateInput.value.trim();
      const timeStr = timeInput.value.trim();

      const date = parseUserInput(dateStr, timeStr);
      if (!date) {
        generatedOutput.value = '';
        showMessage(msgGen, '请输入有效日期或时间', true);
        return;
      }

      const ms = date.getTime();
      const sec = Math.floor(ms / 1000);
      const activeUnit = unitToggle.querySelector('.unit-option.active').dataset.value;
      generatedOutput.value = activeUnit === 'ms' ? ms : sec;
      showMessage(msgGen, '');
    }

    // 监听输入变化(包括手输和选择器)
    dateInput.addEventListener('input', updateFromDateTime);
    timeInput.addEventListener('input', updateFromDateTime);

    unitToggle.querySelectorAll('.unit-option').forEach(btn => {
      btn.addEventListener('click', () => {
        unitToggle.querySelectorAll('.unit-option').forEach(b => b.classList.remove('active'));
        btn.classList.add('active');
        updateFromDateTime();
      });
    });

    function showMessage(el, text, isError = false) {
      el.textContent = text;
      el.className = 'message' + (isError ? ' error' : '');
    }

    function animateCopyButton(btn, successText = '✅ 已复制') {
      const original = btn.textContent;
      btn.classList.add('copied');
      btn.textContent = successText;
      setTimeout(() => {
        btn.classList.remove('copied');
        btn.textContent = original;
      }, 1500);
    }

    const copyMap = {
      copyGenerated: () => generatedOutput.value,
      copyReadable: () => readableTime.value,
      copyParsedMs: () => outputMs.value,
      copyParsedSec: () => outputSec.value
    };

    Object.keys(copyMap).forEach(id => {
      document.getElementById(id).addEventListener('click', async () => {
        const text = copyMap[id]();
        if (!text) {
          showMessage(document.getElementById('msgGen'), '无内容可复制', true);
          return;
        }
        try {
          await navigator.clipboard.writeText(text);
          animateCopyButton(document.getElementById(id));
        } catch (err) {
          showMessage(document.getElementById('msgGen'), '复制失败', true);
        }
      });
    });

    // 时间戳解析
    const inputTimestamp = document.getElementById('inputTimestamp');
    const readableTime = document.getElementById('readableTime');
    const outputMs = document.getElementById('outputMs');
    const outputSec = document.getElementById('outputSec');
    const msgParse = document.getElementById('msgParse');

    inputTimestamp.addEventListener('input', () => {
      let raw = inputTimestamp.value.trim();
      if (!raw) {
        readableTime.value = '';
        outputMs.value = '';
        outputSec.value = '';
        showMessage(msgParse, '');
        return;
      }

      const numStr = raw.replace(/[^0-9]/g, '');
      if (!numStr) {
        showMessage(msgParse, '请输入数字', true);
        readableTime.value = '';
        outputMs.value = '';
        outputSec.value = '';
        return;
      }

      let num = Number(numStr);
      if (isNaN(num)) {
        showMessage(msgParse, '无效数字', true);
        readableTime.value = '';
        outputMs.value = '';
        outputSec.value = '';
        return;
      }

      let ms = numStr.length <= 10 ? num * 1000 : num;

      if (ms < -8640000000000000 || ms > 8640000000000000) {
        showMessage(msgParse, '时间戳超出有效范围', true);
        readableTime.value = '';
        outputMs.value = '';
        outputSec.value = '';
        return;
      }

      const date = new Date(ms);
      if (isNaN(date.getTime())) {
        showMessage(msgParse, '无法解析时间', true);
        readableTime.value = '';
        outputMs.value = '';
        outputSec.value = '';
        return;
      }

      readableTime.value = date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false,
        timeZoneName: 'short'
      });
      outputMs.value = ms;
      outputSec.value = Math.floor(ms / 1000);
      showMessage(msgParse, '');
    });
  </script>
</body>
</html>

五、使用说明

5.1 时间转时间戳

注意:若只填写日期,时间默认为 00:00:00;若只填时间,日期默认为当天。

5.2 时间戳转时间

六、兼容性说明

功能Chrome ≥96Safari ≥16.4Firefox ≥121旧版浏览器
点击输入框弹出选择器❌(需点小箭头)
手动输入
时间戳转换
一键复制✅(需 HTTPS 或 localhost)

即使在不支持 .showPicker() 的环境中,工具依然完全可用,只是选择器唤出方式略有不同。

附:时间戳参考

日期时间毫秒(JavaScript)秒(Unix)
2025-01-01 00:00:0017356608000001735660800
当前时间(示例)17040672000001704067200

以上就是基于JavaScript编写一个时间戳转换工具的详细内容,更多关于JavaScript时间戳转换工具的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文