时间戳转换 #
Unix 时间戳与日期时间互转工具。
什么是 Unix 时间戳? #
Unix 时间戳是从 1970 年 1 月 1 日 00:00:00 UTC 到现在的秒数。
转换方法 #
时间戳转日期 #
JavaScript:
javascript
// 秒级时间戳
const timestamp = 1722067200
const date = new Date(timestamp * 1000)
console.log(date.toLocaleString('zh-CN'))
// 输出: 2024/7/27 15:00:00
// 毫秒级时间戳
const timestampMs = 1722067200000
const date = new Date(timestampMs)
console.log(date.toLocaleString('zh-CN'))
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Python:
python
from datetime import datetime
# 秒级时间戳
timestamp = 1722067200
date = datetime.fromtimestamp(timestamp)
print(date.strftime('%Y-%m-%d %H:%M:%S'))
# 输出: 2024-07-27 15:00:00
1
2
3
4
5
6
7
2
3
4
5
6
7
日期转时间戳 #
JavaScript:
javascript
const date = new Date('2024-07-27 15:00:00')
// 秒级时间戳
const timestamp = Math.floor(date.getTime() / 1000)
console.log(timestamp) // 1722067200
// 毫秒级时间戳
const timestampMs = date.getTime()
console.log(timestampMs) // 1722067200000
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Python:
python
from datetime import datetime
date = datetime(2024, 7, 27, 15, 0, 0)
# 秒级时间戳
timestamp = int(date.timestamp())
print(timestamp) # 1722067200
1
2
3
4
5
6
7
2
3
4
5
6
7
常用时间戳 #
| 日期 | 时间戳 |
|---|---|
| 2024-01-01 00:00:00 | 1704067200 |
| 2024-07-27 00:00:00 | 1722038400 |
| 2025-01-01 00:00:00 | 1735689600 |