
? Unix 时间戳文本互转工具使用指南:秒毫秒精度时区实时转换
?️ 在线工具:简单快捷不用装软件
? Epoch Converter:支持多种格式和时区
- 打开浏览器,输入网址
https://www.epochconverter.com/
,直接就进入主页了。 - 左边 “Date and Time” 这块,选好你用的时间格式,像 “YYYY - MM - DD HH:MM:SS” 这种常见的格式都有。
- 输入你要转换的时间,比如 “2025 - 06 - 17 16:00:00”,注意这里默认是 UTC 时区哦。要是你想用其他时区,点右边 “Time Zone” 的下拉菜单,找到 “Shanghai (UTC + 08:00)” 这种你需要的时区。
- 下边 “Output Formats” 里,选 “Unix Epoch (seconds)” 就是秒级时间戳,选 “Unix Epoch (milliseconds)” 就是毫秒级时间戳。
- 点 “Convert” 按钮,右边马上就会显示出转换后的时间戳,比如秒级可能是 “1750118400”,毫秒级就是 “1750118400000”。
- 同样打开网站,这次在右边 “Epoch Timestamp” 输入框里,填上你要转换的时间戳,比如 “1750118400”。
- 选好时间戳的单位,是秒还是毫秒,就在输入框下面的选项里选。
- 时区还是按需选择,然后点 “Convert”,左边就会出现对应的文本时间啦。
? 在线工具对比:各有啥特点
https://www.unixtimestamp.com/
,界面稍微复杂一点,但是功能更多,能显示时间戳在不同时区的对应时间,还能生成时间戳的图表。还有 https://timestampconverter.guru/
,支持更多的时间格式,像 “DD/MM/YYYY HH:MM:SS” 这种欧洲常用的格式也能处理。大家可以根据自己的习惯选,不过要注意,用在线工具的时候,别把敏感的时间戳数据上传到不可信的网站哦。? 命令行工具:程序员的高效选择
? Linux/macOS 下的 date 命令:强大的本地转换
在终端输入命令:
date -j -f "%Y-%m-%d %H:%M:%S" "2025-06-17 16:00:00" +%s
date -d "2025-06-17 16:00:00" +%s
输入命令:
date -r "+%Y-%m-%d %H:%M:%S"
秒级时间戳转毫秒很简单,就是乘以 1000,比如 “1750118400” 秒等于 “1750118400000” 毫秒。那怎么把毫秒级时间戳转成文本呢?先把毫秒数除以 1000 得到秒数,再用上面的命令。比如毫秒级时间戳 “1750118400000”,先算 “1750118400000 / 1000 = 1750118400”,再用 date 命令转换。
? Windows 下的 PowerShell:轻松实现转换
[DateTime]::ToUniversalTime([DateTime]'2025-06-17 16:00:00').Subtract([DateTime]::new(1970, 1, 1, 0, 0, 0, [DateTimeKind]::Utc)).TotalSeconds
$epoch = [DateTime]::new(1970, 1, 1, 0, 0, 0, [DateTimeKind]::Utc)
$timestamp = 1750118400
$epoch.AddSeconds($timestamp).ToString("yyyy-MM-dd HH:mm:ss")
? 编程语言库:开发场景必备
? Python:time 和 datetime 模块
from datetime import datetime
time_str = "2025-06-17 16:00:00"
# 转成本地时区的时间戳
local_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
local_timestamp = int(local_time.timestamp())
print(local_timestamp) # 输出秒级时间戳
# 转成 UTC 时区的时间戳
utc_time = local_time.astimezone(datetime.timezone.utc)
utc_timestamp = int(utc_time.timestamp())
print(utc_timestamp)
import time
time_str = "2025-06-17 16:00:00"
time_struct = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
millisecond_timestamp = int(time.mktime(time_struct)) *
print(millisecond_timestamp)
from datetime import datetime
timestamp =
# 本地时区
local_time = datetime.fromtimestamp(timestamp)
print(local_time.strftime("%Y-%m-%d %H:%M:%S"))
# UTC 时区
utc_time = datetime.utcfromtimestamp(timestamp)
print(utc_time.strftime("%Y-%m-%d %H:%M:%S"))
from datetime import datetime
millisecond_timestamp =
# 先转成秒级
timestamp = millisecond_timestamp //
# 处理毫秒部分
milliseconds = millisecond_timestamp %
utc_time = datetime.utcfromtimestamp(timestamp)
print(utc_time.strftime("%Y-%m-%d %H:%M:%S.%f")[:-] + f"{milliseconds:03d}")
pytz
或者 zoneinfo
库,能更准确地处理时区偏移和夏令时等问题。? JavaScript:Date 对象灵活处理
const timeStr = "2025-06-17T16:00:00"; // ISO 8601 格式,T 分隔日期和时间
const date = new Date(timeStr);
// 毫秒级时间戳,Date.now() 返回的就是当前时间的毫秒级时间戳
const millisecondTimestamp = date.getTime();
console.log(millisecondTimestamp);
// 如果文本时间不是 UTC,转成 UTC 时间戳
const utcDate = new Date(date.toISOString());
const utcMillisecondTimestamp = utcDate.getTime();
console.log(utcMillisecondTimestamp);
const millisecondTimestamp = ;
const date = new Date(millisecondTimestamp);
// 转成本地时区的文本时间
const localTimeStr = date.toLocaleString(); // 格式可能因浏览器而异
// 自定义格式
const year = date.getFullYear();
const month = String(date.getMonth() + ).padStart(, '0');
const day = String(date.getDate()).padStart(, '0');
const hour = String(date.getHours()).padStart(, '0');
const minute = String(date.getMinutes()).padStart(, '0');
const second = String(date.getSeconds()).padStart(, '0');
const localTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
console.log(localTime);
// 转成 UTC 时间
const utcYear = date.getUTCFullYear();
const utcMonth = String(date.getUTCMonth() + ).padStart(, '0');
const utcDay = String(date.getUTCDate()).padStart(, '0');
const utcHour = String(date.getUTCHours()).padStart(, '0');
const utcMinute = String(date.getUTCMinutes()).padStart(, '0');
const utcSecond = String(date.getUTCSeconds()).padStart(, '0');
const utcTime = `${utcYear}-${utcMonth}-${utcDay} ${utcHour}:${utcMinute}:${utcSecond}`;
console.log(utcTime);
? Java:java.time 包处理现代时间
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.Instant;
String timeStr = "2025-06-17T16:00:00";
LocalDateTime localDateTime = LocalDateTime.parse(timeStr);
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
Instant instant = zonedDateTime.toInstant();
long secondTimestamp = instant.getEpochSecond(); // 秒级时间戳
System.out.println(secondTimestamp);
// 转成 UTC 时间戳
Instant utcInstant = Instant.parse(timeStr + "Z"); // Z 表示 UTC 时区
long utcSecondTimestamp = utcInstant.getEpochSecond();
System.out.println(utcSecondTimestamp);
long secondTimestamp = ;
Instant instant = Instant.ofEpochSecond(secondTimestamp);
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Shanghai"));
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
String localTimeStr = localDateTime.toString(); // 格式为 ISO 8601,可自定义格式
System.out.println(localTimeStr);
// UTC 时间
ZonedDateTime utcZonedDateTime = instant.atZone(ZoneId.utc());
LocalDateTime utcLocalDateTime = utcZonedDateTime.toLocalDateTime();
String utcTimeStr = utcLocalDateTime.toString();
System.out.println(utcTimeStr);
⚠️ 常见问题和注意事项
? 精度问题:秒和毫秒别搞混
? 时区问题:统一标准避免混乱
? 批量处理:写脚本提高效率
? 工具选择:根据场景选合适的
? 总结:不同场景下的最佳工具
场景 | 推荐工具 | 优势 | 注意事项 |
---|---|---|---|
临时单个转换 | Epoch Converter | 无需安装,界面直观 | 注意时区和精度选择 |
批量文件处理 | Linux date 命令 / PowerShell | 可写脚本,高效批量处理 | 需熟悉命令参数 |
开发集成 | 编程语言库(Python/JS/Java) | 无缝融入代码,功能强大 | 注意时区和精度的代码实现细节 |
Windows 用户 | PowerShell | 系统自带,无需额外安装 | 命令语法和 Linux 略有不同 |