时间戳转换工具 / TIME INDEX
在可读日期与 Unix 数字之间双向转换,看清楚这个时间在本地是什么、在全球时间线上是什么。时间戳基于 UTC,秒与毫秒是两种常见刻度,本工具帮你少犯单位与时区错误。
本地处理 · 不上传
路径 /tools/timestamp
日期时间 → 时间戳
DATETIME → UNIX · 浏览器本地时区
主输出
秒级—
毫秒级—
等待转换
时间戳 → 日期时间
UNIX → DATETIME · 自动检测秒/毫秒
检测:—
本地时间—
UTC (ISO)—
等待转换
长度规则
10 位:秒级精度,常见于 Unix 时间戳与多数后端 API。13 位:毫秒级精度,常见于 JavaScriptDate.now()与 JavaSystem.currentTimeMillis()。- JavaScript
Date只有毫秒精度,本工具不显示真实微秒或纳秒。
常见错误
- 长度错误:输入不是 10 位或 13 位(例如 11、12 位)。
- 非法字符:包含字母、空格或符号;本工具不会静默删除,会直接报错。
- 负时间戳:本工具暂不支持负值(不静默去掉
-)。 - 范围错误:超出 JavaScript
Date可表示范围。
01 时间知识记录 Unix Epoch · UTC · 本地时间
Unix Epoch
1970-01-01 00:00:00 UTC
秒(s)
10 位整数
毫秒(ms)
13 位整数
现在
-
UTC
时间戳基于协调世界时(UTC),不随时区变化。同一个时间戳在任何时区都指向同一瞬间。
本地时间
同一个时间戳在不同时区显示为不同的墙上时间。本工具的本地时间来自浏览器当前时区,文案标注为「浏览器本地时区(UTC±x)」。
秒与毫秒
10 位通常是秒,13 位通常是毫秒。混淆两者会让结果偏差 1000 倍,这是时间戳最常见的 bug。
02 编程语言示例 JS · Python · Java · Go · PHP
// 获取当前时间戳(毫秒)
const timestampMs = Date.now();
// 获取当前时间戳(秒)
const timestamp = Math.floor(Date.now() / 1000);
// 时间戳(毫秒)转日期:1736553600000 = 2025-01-11 08:00:00 (UTC+8)
const date = new Date(1736553600000);
console.log(date.toLocaleString('zh-CN')); // 2025/1/11 08:00:00 (UTC+8)
// 日期转时间戳(按本地时区解析 ISO 本地时间)
const newDate = new Date('2025-01-11T08:00:00');
console.log(newDate.getTime()); // 1736553600000(在 UTC+8 下)
import time
from datetime import datetime
# 获取当前时间戳(秒)
timestamp = int(time.time())
# 获取当前时间戳(毫秒)
timestamp_ms = int(time.time() * 1000)
# 时间戳(秒)转日期时间:1736553600 = 2025-01-11 08:00:00 (UTC+8)
dt = datetime.fromtimestamp(1736553600)
print(dt.strftime('%Y-%m-%d %H:%M:%S')) # 2025-01-11 08:00:00
# 日期时间转时间戳(按本地时区)
dt = datetime.strptime('2025-01-11 08:00:00', '%Y-%m-%d %H:%M:%S')
print(int(dt.timestamp())) # 1736553600(在 UTC+8 下)
import java.time.*;
import java.time.format.DateTimeFormatter;
// 获取当前时间戳(秒)
long timestamp = Instant.now().getEpochSecond();
// 获取当前时间戳(毫秒)
long timestampMs = System.currentTimeMillis();
// 时间戳(秒)转日期时间:1736553600 = 2025-01-11 08:00:00 (UTC+8)
LocalDateTime dateTime = LocalDateTime.ofEpochSecond(
1736553600, 0, ZoneOffset.ofHours(8)
);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(dateTime.format(formatter)); // 2025-01-11 08:00:00
// 日期时间转时间戳(指定时区 UTC+8)
LocalDateTime dt = LocalDateTime.parse("2025-01-11 08:00:00", formatter);
long ts = dt.toEpochSecond(ZoneOffset.ofHours(8));
System.out.println(ts); // 1736553600
package main
import (
"fmt"
"time"
)
func main() {
// 获取当前时间戳(秒)
timestamp := time.Now().Unix()
// 获取当前时间戳(毫秒)
timestampMs := time.Now().UnixMilli()
// 时间戳(秒)转日期时间:1736553600 = 2025-01-11 08:00:00 (UTC+8)
t := time.Unix(1736553600, 0)
fmt.Println(t.Format("2006-01-02 15:04:05")) // 2025-01-11 08:00:00 (在 UTC+8 下)
// 日期时间转时间戳(指定时区 Asia/Shanghai)
layout := "2006-01-02 15:04:05"
loc, _ := time.LoadLocation("Asia/Shanghai")
dt, _ := time.ParseInLocation(layout, "2025-01-11 08:00:00", loc)
fmt.Println(dt.Unix()) // 1736553600
}
<?php
// 获取当前时间戳(秒)
$timestamp = time();
// 获取当前时间戳(毫秒)
$timestampMs = round(microtime(true) * 1000);
// 时间戳(秒)转日期时间:1736553600 = 2025-01-11 08:00:00 (UTC+8)
// 需 date.timezone = Asia/Shanghai
$date = date('Y-m-d H:i:s', 1736553600);
echo $date; // 2025-01-11 08:00:00
// 日期时间转时间戳(按服务器时区)
$ts = strtotime('2025-01-11 08:00:00');
echo $ts; // 1736553600(在 Asia/Shanghai 下)
?>
示例中 1736553600 秒 / 1736553600000 毫秒对应 2025-01-11 08:00:00 (UTC+8),即 2025-01-11 00:00:00 UTC。各语言按本地或指定时区解析时结果一致。