🌸
💫
跳转到内容
非官方中文文档 彩虹工具箱

时间戳转换

功能强大的时间戳转换工具,支持 Unix 时间戳与各种日期格式互转。

Unix 时间戳(Unix Timestamp)是从 1970 年 1 月 1 日 00:00:00 UTC 开始所经过的秒数(或毫秒数)。

单位示例说明
秒级170406720010 位数字
毫秒级170406720000013 位数字
微秒级170406720000000016 位数字
  • 🔄 双向转换 - 时间戳 ↔ 日期时间互转
  • 🌍 时区支持 - 支持全球主要时区
  • 📅 多种格式 - ISO 8601、RFC 2822、本地格式
  • ⏱️ 实时显示 - 显示当前时间戳
  • 🧮 时间计算 - 加减时间、计算时间差
  • 📋 批量转换 - 同时处理多个时间戳
  1. 输入 Unix 时间戳:
1704067200
  1. 选择单位(秒/毫秒)
  2. 选择时区(UTC/本地/其他)
  3. 获取转换结果:
北京时间: 2024-01-01 08:00:00
UTC时间: 2024-01-01 00:00:00
  1. 输入日期时间:
2024-01-01 08:00:00
  1. 选择输入时区
  2. 获取时间戳:
秒级: 1704067200
毫秒级: 1704067200000

工具自动识别输入的时间戳格式:

// 10 位 - 秒级
17040672002024-01-01 00:00:00 UTC
// 13 位 - 毫秒级
17040672000002024-01-01 00:00:00.000 UTC
// 16 位 - 微秒级
17040672000000002024-01-01 00:00:00.000000 UTC

支持全球主要时区:

时区时差示例
UTC+02024-01-01 00:00:00
北京时间 (CST)+82024-01-01 08:00:00
东京 (JST)+92024-01-01 09:00:00
纽约 (EST)-52023-12-31 19:00:00
伦敦 (GMT)+02024-01-01 00:00:00
悉尼 (AEDT)+112024-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"

支持多种输出格式:

2024-01-01T08:00:00+08:00
2024-01-01T00:00:00Z
Mon, 01 Jan 2024 08:00:00 +0800
2024年1月1日 08:00:00
2024/01/01 08:00:00
01/01/2024 08:00:00
YYYY-MM-DD HH:mm:ss → 2024-01-01 08:00:00
YYYY/MM/DD → 2024/01/01
MM-DD-YYYY HH:mm → 01-01-2024 08:00
// 当前时间戳
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:00
const 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 小时"
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);
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);
const now = new Date();
// 本月开始
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
// 本月结束
const endOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);

显示当前实时时间戳:

当前时间戳(秒): 1704067200
当前时间戳(毫秒): 1704067200000
北京时间: 2024-01-01 08:00:00
UTC时间: 2024-01-01 00:00:00
Unix时间: 1704067200

后端返回时间戳,前端转换为可读日期:

// 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"
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-01
countdown(target);
// "30天 12时 34分 56秒"

分析日志文件中的时间戳:

[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
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 有效');
}

查询某天的数据:

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 < 1704153600

A:

  • 10 位数字 - 秒级(Unix 标准)
  • 13 位数字 - 毫秒级(JavaScript 标准)
  • 16 位数字 - 微秒级

JavaScript 中使用的是毫秒级,而许多后端 API 使用秒级。

A: 检查:

  • 输入时间戳的时区(通常是 UTC)
  • 目标时区的夏令时设置
  • 使用 toLocaleString 时指定正确的 timeZone

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)

A: 有!JavaScript 中 Date 对象的时间戳范围:

  • 最小值: -8640000000000000(公元前 271821 年)
  • 最大值: 8640000000000000(公元 275760 年)

实际使用中的 32 位时间戳将在 2038 年溢出(Year 2038 Problem)。

Q: 为什么我的时间戳转换结果不对?

Section titled “Q: 为什么我的时间戳转换结果不对?”

A: 常见原因:

  • 秒级/毫秒级混淆(差 1000 倍)
  • 时区设置错误
  • 输入格式不正确
  • 使用了本地时间而非 UTC