学习笔记:Python 时区时间戳与前端 Day.js 转换指南
📌 核心概念与避坑指南
在进行前后端时间交互时,必须牢记以下两个核心原则:
- 时间戳是绝对的:时间戳(Timestamp)本质上是距离
1970-01-01 00:00:00 UTC的时间差。它没有时区概念,全球统一。所谓的“东8区时间戳”,是指“将东8区的某个时间,转换为全球统一的时间戳”。 - 单位差异(巨坑):
- Python 的时间戳:单位是 秒 (s)(10 位整数或浮点数,如
1715148240)。 - JavaScript 的时间戳:单位是 毫秒 (ms)(13 位整数,如
1715148240000)。 - 结论:前端接收到 Python 时间戳后,必须乘以 1000,否则时间会变成 1970 年!
- Python 的时间戳:单位是 秒 (s)(10 位整数或浮点数,如
🐍 第一部分:Python 获取指定时区(东8区)时间戳
在跨时区业务中,强烈建议显式指定时区,不要依赖服务器的系统时区设置。Python 3.9+ 推荐使用内置的 zoneinfo 模块。
场景 1:获取当前时间的东8区时间戳
from datetime import datetime
from zoneinfo import ZoneInfo
# 1. 显式指定时区为亚洲/上海 (UTC+8)
tz_shanghai = ZoneInfo("Asia/Shanghai")
# 2. 获取带时区信息的当前时间 (Aware Datetime)
dt_now = datetime.now(tz_shanghai)
# 3. 转换为时间戳 (秒)
timestamp_sec = dt_now.timestamp()
print(f"当前东8区时间: {dt_now}")
print(f"Python时间戳(秒): {int(timestamp_sec)}") # 输出例: 1715148240
场景 2:将指定的字符串时间转为东8区时间戳
from datetime import datetime
from zoneinfo import ZoneInfo
time_str = "2026-05-08 14:00:00"
# 1. 将字符串解析为无时区时间 (Naive Datetime)
dt_naive = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
# 2. 强制赋予东8区时区信息 (Aware Datetime)
dt_utc8 = dt_naive.replace(tzinfo=ZoneInfo("Asia/Shanghai"))
# 3. 获取时间戳
timestamp_sec = dt_utc8.timestamp()
print(f"对应时间戳: {int(timestamp_sec)}")
🌐 第二部分:前端 (JS / Day.js) 解析 Python 时间戳
前端接收到后端的 10 位秒级时间戳后,需要将其转换为 JS 的 Date 对象或 Day.js 对象进行渲染。
方法 1:使用 Day.js(现代前端推荐)
Day.js 提供了专门针对秒级时间戳的 API,非常优雅。
import dayjs from 'dayjs';
// 假设这是后端传来的 Python 时间戳 (秒)
const pythonTimestamp = 1715148240;
// ✨ 方案 A:使用 .unix() 方法(最优雅,专门解析秒级时间戳)
const dateObj = dayjs.unix(pythonTimestamp);
// 方案 B:手动乘 1000
const dateObj2 = dayjs(pythonTimestamp * 1000);
// 格式化输出页面
console.log(dateObj.format('YYYY-MM-DD HH:mm:ss'));
// 如果 Ant Design 等组件需要原生的 JS Date 对象:
const nativeDate = dateObj.toDate();
方法 2:使用原生 JavaScript Date 对象
如果不使用第三方库,必须手动乘以 1000。
const pythonTimestamp = 1715148240;
// ⚠️ 必须乘 1000 转换为毫秒
const jsDate = new Date(pythonTimestamp * 1000);
console.log(jsDate.toLocaleString());
📝 总结速查表
| 动作 | 代码片段 | 备注 |
|---|---|---|
| Python 设时区 | dt.replace(tzinfo=ZoneInfo("Asia/Shanghai")) | Python 3.9+ 推荐用法 |
| Python 转时间戳 | dt.timestamp() | 返回浮点数,单位为秒 |
| JS 原生解析 | new Date(py_ts * 1000) | 必须 乘 1000 |
| Day.js 解析 | dayjs.unix(py_ts) | 使用 .unix() 自动处理秒级单位 |
| Day.js 转原生 | dayjsObj.toDate() | 适配 antd 等 UI 组件库 |