时间戳转换
时间戳转换工具 ⏰
Section titled “时间戳转换工具 ⏰”功能强大的时间戳转换工具,支持 Unix 时间戳与各种日期格式互转。
什么是 Unix 时间戳?
Section titled “什么是 Unix 时间戳?”Unix 时间戳(Unix Timestamp)是从 1970 年 1 月 1 日 00:00:00 UTC 开始所经过的秒数(或毫秒数)。
| 单位 | 示例 | 说明 |
|---|---|---|
| 秒级 | 1704067200 | 10 位数字 |
| 毫秒级 | 1704067200000 | 13 位数字 |
| 微秒级 | 1704067200000000 | 16 位数字 |
- 🔄 双向转换 - 时间戳 ↔ 日期时间互转
- 🌍 时区支持 - 支持全球主要时区
- 📅 多种格式 - ISO 8601、RFC 2822、本地格式
- ⏱️ 实时显示 - 显示当前时间戳
- 🧮 时间计算 - 加减时间、计算时间差
- 📋 批量转换 - 同时处理多个时间戳
时间戳转日期
Section titled “时间戳转日期”- 输入 Unix 时间戳:
1704067200- 选择单位(秒/毫秒)
- 选择时区(UTC/本地/其他)
- 获取转换结果:
北京时间: 2024-01-01 08:00:00UTC时间: 2024-01-01 00:00:00日期转时间戳
Section titled “日期转时间戳”- 输入日期时间:
2024-01-01 08:00:00- 选择输入时区
- 获取时间戳:
秒级: 1704067200毫秒级: 17040672000001. 时间戳格式识别
Section titled “1. 时间戳格式识别”工具自动识别输入的时间戳格式:
// 10 位 - 秒级1704067200 → 2024-01-01 00:00:00 UTC
// 13 位 - 毫秒级1704067200000 → 2024-01-01 00:00:00.000 UTC
// 16 位 - 微秒级1704067200000000 → 2024-01-01 00:00:00.000000 UTC2. 时区转换
Section titled “2. 时区转换”支持全球主要时区:
| 时区 | 时差 | 示例 |
|---|---|---|
| UTC | +0 | 2024-01-01 00:00:00 |
| 北京时间 (CST) | +8 | 2024-01-01 08:00:00 |
| 东京 (JST) | +9 | 2024-01-01 09:00:00 |
| 纽约 (EST) | -5 | 2023-12-31 19:00:00 |
| 伦敦 (GMT) | +0 | 2024-01-01 00:00:00 |
| 悉尼 (AEDT) | +11 | 2024-01-01 11:00:00 |
转换示例:
// 时间戳 1704067200(UTC)const timestamp = 1704067200;
// 转换为不同时区new Date(timestamp * 1000).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });// "2024/1/1 08:00:00"
new Date(timestamp * 1000).toLocaleString('en-US', { timeZone: 'America/New_York' });// "12/31/2023, 7:00:00 PM"3. 日期格式输出
Section titled “3. 日期格式输出”支持多种输出格式:
ISO 8601 格式
Section titled “ISO 8601 格式”2024-01-01T08:00:00+08:002024-01-01T00:00:00ZRFC 2822 格式
Section titled “RFC 2822 格式”Mon, 01 Jan 2024 08:00:00 +08002024年1月1日 08:00:002024/01/01 08:00:0001/01/2024 08:00:00YYYY-MM-DD HH:mm:ss → 2024-01-01 08:00:00YYYY/MM/DD → 2024/01/01MM-DD-YYYY HH:mm → 01-01-2024 08:004. 时间计算
Section titled “4. 时间计算”// 当前时间戳const now = Date.now();
// 加 1 天const tomorrow = now + 24 * 60 * 60 * 1000;
// 减 1 小时const oneHourAgo = now - 60 * 60 * 1000;
// 加 30 分钟const thirtyMinLater = now + 30 * 60 * 1000;计算两个时间的差值:
const start = 1704067200000; // 2024-01-01 08:00:00const end = 1704153600000; // 2024-01-02 08:00:00
const diff = end - start;// 86400000 毫秒 = 1 天
// 转换为友好格式const days = Math.floor(diff / (1000 * 60 * 60 * 24));const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));// "1 天 0 小时"5. 特殊时间获取
Section titled “5. 特殊时间获取”当天开始/结束
Section titled “当天开始/结束”const now = new Date();
// 当天开始(00:00:00)const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
// 当天结束(23:59:59)const endOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59);本周开始/结束
Section titled “本周开始/结束”const now = new Date();const dayOfWeek = now.getDay(); // 0 = 周日, 1 = 周一
// 本周一const startOfWeek = new Date(now);startOfWeek.setDate(now.getDate() - dayOfWeek + 1);startOfWeek.setHours(0, 0, 0, 0);
// 本周日const endOfWeek = new Date(startOfWeek);endOfWeek.setDate(startOfWeek.getDate() + 6);endOfWeek.setHours(23, 59, 59, 999);本月开始/结束
Section titled “本月开始/结束”const now = new Date();
// 本月开始const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
// 本月结束const endOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);6. 实时时钟
Section titled “6. 实时时钟”显示当前实时时间戳:
当前时间戳(秒): 1704067200当前时间戳(毫秒): 1704067200000
北京时间: 2024-01-01 08:00:00UTC时间: 2024-01-01 00:00:00Unix时间: 1704067200实际应用场景
Section titled “实际应用场景”场景 1:API 时间处理
Section titled “场景 1:API 时间处理”后端返回时间戳,前端转换为可读日期:
// API 返回const apiResponse = { created_at: 1704067200, // Unix 时间戳(秒) updated_at: 1704153600000 // Unix 时间戳(毫秒)};
// 前端转换function formatTimestamp(timestamp) { // 统一转为毫秒 const ms = timestamp.toString().length === 10 ? timestamp * 1000 : timestamp; const date = new Date(ms);
return date.toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' });}
formatTimestamp(apiResponse.created_at);// "2024/01/01 08:00:00"场景 2:倒计时功能
Section titled “场景 2:倒计时功能”function countdown(targetTimestamp) { const now = Date.now(); const diff = targetTimestamp - now;
if (diff <= 0) return '已结束';
const days = Math.floor(diff / (1000 * 60 * 60 * 24)); const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); const seconds = Math.floor((diff % (1000 * 60)) / 1000);
return `${days}天 ${hours}时 ${minutes}分 ${seconds}秒`;}
// 使用const target = 1706745600000; // 2024-02-01countdown(target);// "30天 12时 34分 56秒"场景 3:日志时间戳转换
Section titled “场景 3:日志时间戳转换”分析日志文件中的时间戳:
[1704067200] User login: user123[1704067260] User action: click_button[1704067320] User logout: user123批量转换为可读时间:
const logLines = [ '[1704067200] User login: user123', '[1704067260] User action: click_button', '[1704067320] User logout: user123'];
logLines.map(line => { const match = line.match(/\[(\d+)\]/); if (match) { const timestamp = parseInt(match[1]) * 1000; const date = new Date(timestamp).toLocaleString('zh-CN'); return line.replace(match[0], `[${date}]`); } return line;});
// 结果:// [2024/01/01 08:00:00] User login: user123// [2024/01/01 08:01:00] User action: click_button// [2024/01/01 08:02:00] User logout: user123场景 4:Token 过期检查
Section titled “场景 4:Token 过期检查”function isTokenExpired(expireTimestamp) { const now = Math.floor(Date.now() / 1000); // 当前秒级时间戳 return now >= expireTimestamp;}
// JWT 中的 exp 字段const tokenPayload = { exp: 1706745600 // 2024-02-01 00:00:00 UTC};
if (isTokenExpired(tokenPayload.exp)) { console.log('Token 已过期,需要重新登录');} else { console.log('Token 有效');}场景 5:数据查询范围
Section titled “场景 5:数据查询范围”查询某天的数据:
function getDayRange(dateString) { const date = new Date(dateString);
// 当天开始 const start = new Date(date.getFullYear(), date.getMonth(), date.getDate()); // 当天结束 const end = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
return { start: Math.floor(start.getTime() / 1000), // 秒级时间戳 end: Math.floor(end.getTime() / 1000) };}
// 查询 2024-01-01 的数据const range = getDayRange('2024-01-01');// { start: 1704067200, end: 1704153600 }
// SQL 查询// SELECT * FROM orders WHERE created_at >= 1704067200 AND created_at < 1704153600Q: 时间戳是秒还是毫秒?
Section titled “Q: 时间戳是秒还是毫秒?”A:
- 10 位数字 - 秒级(Unix 标准)
- 13 位数字 - 毫秒级(JavaScript 标准)
- 16 位数字 - 微秒级
JavaScript 中使用的是毫秒级,而许多后端 API 使用秒级。
Q: 时区转换出错?
Section titled “Q: 时区转换出错?”A: 检查:
- 输入时间戳的时区(通常是 UTC)
- 目标时区的夏令时设置
- 使用
toLocaleString时指定正确的 timeZone
Q: 如何处理夏令时?
Section titled “Q: 如何处理夏令时?”A: 现代浏览器和操作系统会自动处理夏令时:
// 夏令时期间new Date('2024-07-01').toLocaleString('en-US', { timeZone: 'America/New_York' });// 自动使用 EDT (UTC-4)
// 标准时间期间new Date('2024-01-01').toLocaleString('en-US', { timeZone: 'America/New_York' });// 自动使用 EST (UTC-5)Q: 时间戳有最大值吗?
Section titled “Q: 时间戳有最大值吗?”A: 有!JavaScript 中 Date 对象的时间戳范围:
- 最小值: -8640000000000000(公元前 271821 年)
- 最大值: 8640000000000000(公元 275760 年)
实际使用中的 32 位时间戳将在 2038 年溢出(Year 2038 Problem)。
Q: 为什么我的时间戳转换结果不对?
Section titled “Q: 为什么我的时间戳转换结果不对?”A: 常见原因:
- 秒级/毫秒级混淆(差 1000 倍)
- 时区设置错误
- 输入格式不正确
- 使用了本地时间而非 UTC