当前 Unix 时间戳的含义
当前 Unix 时间戳是自 1970 年 1 月 1 日 00:00:00 UTC 起经过的整秒数。实时 epoch 时钟每秒递增一次。如果时钟显示毫秒,则每秒递增多次,通常是 13 位的值。
为什么以 UTC 为基准
Unix 时间以 UTC 为基础,使不同时区的计算机能就同一时刻达成一个数值上的一致。本地时间只是显示层。两个用户在同一时刻加载页面时,即使一个看到早晨、另一个看到傍晚,他们也共享相同的当前 Unix 时间戳。
如何获取当前 Unix 时间戳
每种常见语言都有获取当前 Unix 时间的简短方法。
- JavaScript 毫秒:Date.now()
- JavaScript 秒:Math.floor(Date.now() / 1000)
- Linux 秒:date +%s
- Python 秒:int(time.time())
- PHP 秒:time()
- Go 秒:time.Now().Unix()
- PostgreSQL 秒:EXTRACT(EPOCH FROM now())
当前时间戳 vs 格式化时间
时间戳适合排序、比较、存储和 API 负载。格式化日期适合人阅读。大多数应用存储时间戳或 UTC 日期时间,只有在向用户显示时才进行格式化。
- 在数据库和日志中存储 UTC 或 Unix 时间戳
- 以用户所在时区显示本地时间
- 当人需要检查 API 负载时使用 ISO 8601
- 当系统需要紧凑的数值时使用 Unix 秒或毫秒
FAQ
- Does the current Unix timestamp depend on my timezone?
- No. The timestamp is based on UTC and is the same worldwide for the same instant.
- Why is my current timestamp 13 digits?
- A 13-digit current timestamp is usually milliseconds. Divide by 1000 to get Unix seconds.
- Where can I see a live Unix epoch clock?
- The converter homepage shows a live epoch clock and lets you convert the value to UTC or local time.
- Is the current Unix timestamp the same in every programming language?
- Yes for the instant, but not always for the unit. JavaScript's Date.now() and Java's System.currentTimeMillis() return milliseconds, while Python time.time(), PHP time(), Go Unix(), and Linux date +%s return seconds. Confirm the unit before comparing or storing values.