(function() { let timer = null; let isPaused = false; function initTimeTool() { const container = document.getElementById('tool-time-container'); if (!container) return; container.addEventListener('click', (e) => e.stopPropagation()); // Cleanup if (timer) clearInterval(timer); // DOM Elements const currentNow = document.getElementById('current-now'); const unixS = document.getElementById('current-unix-s'); const unixMs = document.getElementById('current-unix-ms'); const toggleBtn = document.getElementById('toggle-pause'); const inputTs = document.getElementById('input-timestamp'); const btnConvertTs = document.getElementById('btn-convert-ts'); const inputDate = document.getElementById('input-date'); const btnConvertDate = document.getElementById('btn-convert-date'); const copyBtns = document.querySelectorAll('.copy-btn'); // Initial Setup const now = new Date(); now.setMinutes(now.getMinutes() - now.getTimezoneOffset()); inputDate.value = now.toISOString().slice(0, 19); // Event Listeners toggleBtn.addEventListener('click', () => { isPaused = !isPaused; toggleBtn.innerText = isPaused ? '▶️ 恢复更新' : '⏸️ 暂停更新'; }); btnConvertTs.addEventListener('click', convertTimestamp); btnConvertDate.addEventListener('click', convertDate); copyBtns.forEach(btn => { btn.addEventListener('click', (e) => { const targetId = e.target.getAttribute('data-copy-target'); const text = document.getElementById(targetId).innerText; navigator.clipboard.writeText(text).then(() => { const originalText = e.target.innerText; e.target.innerText = '已复制'; setTimeout(() => e.target.innerText = originalText, 1000); }); }); }); // Start Loop timer = setInterval(() => { if (isPaused) return; const d = new Date(); currentNow.innerText = d.toLocaleString(); unixS.innerText = Math.floor(d.getTime() / 1000); unixMs.innerText = d.getTime(); updateWorldClocks(d); }, 1000); // Initial run currentNow.innerText = new Date().toLocaleString(); updateWorldClocks(new Date()); } function convertTimestamp() { let val = document.getElementById('input-timestamp').value.trim(); if (!val) return; let unit = document.getElementById('timestamp-unit').value; let ts = parseInt(val); if (isNaN(ts)) return; if (unit === 'auto') { unit = String(ts).length > 11 ? 'ms' : 's'; } const date = new Date(unit === 's' ? ts * 1000 : ts); document.getElementById('result-date').innerHTML = ` 北京时间: ${date.toLocaleString('zh-CN', {timeZone: 'Asia/Shanghai'})}
UTC 时间: ${date.toUTCString()}
ISO 8601: ${date.toISOString()} `; } function convertDate() { const val = document.getElementById('input-date').value; if (!val) return; const date = new Date(val); const tsMs = date.getTime(); const tsS = Math.floor(tsMs / 1000); document.getElementById('result-timestamp').innerHTML = ` 时间戳 (秒): ${tsS}
时间戳 (毫秒): ${tsMs} `; } function updateWorldClocks(now) { const grid = document.getElementById('world-clocks'); if (!grid) return; const cities = [ { name: '北京', tz: 'Asia/Shanghai' }, { name: '伦敦', tz: 'Europe/London' }, { name: '纽约', tz: 'America/New_York' }, { name: '东京', tz: 'Asia/Tokyo' }, { name: '巴黎', tz: 'Europe/Paris' }, { name: '悉尼', tz: 'Australia/Sydney' }, { name: '迪拜', tz: 'Asia/Dubai' }, { name: '洛杉矶', tz: 'America/Los_Angeles' } ]; let html = ''; cities.forEach(city => { const timeStr = now.toLocaleTimeString('en-US', { timeZone: city.tz, hour12: false, hour: '2-digit', minute: '2-digit' }); html += `
${city.name}
${timeStr}
`; }); grid.innerHTML = html; } // Init document.addEventListener('DOMContentLoaded', initTimeTool); document.addEventListener('pjax:complete', initTimeTool); })();