什么是 epoch 毫秒
epoch 毫秒计数自 1970 年 1 月 1 日 00:00:00 UTC 起的毫秒。它使用与 Unix 秒相同的 Unix 纪元,但精度多三位。现代的 epoch 毫秒值通常为 13 位,如 1700000000000。
如何把 millis 转为日期
如果值已经是毫秒,JavaScript Date 可以直接读取。如果接收工具期望秒,请先除以 1000。主要错误是在错误的边界上做乘除。
- JavaScript: new Date(1700000000000).toISOString()
- Milliseconds to seconds: Math.floor(1700000000000 / 1000)
- Seconds to milliseconds: 1700000000 * 1000
- Java Instant: Instant.ofEpochMilli(1700000000000L)
- Python: datetime.fromtimestamp(ms / 1000, tz=timezone.utc)
在 Java 和数据库中把 long 转为日期
搜索「long 转日期」通常来自 Java、Kotlin、Android,或把时间戳存为 64 位整数的数据库导出。该值可能是 epoch 毫秒,但也可能是秒、微秒或纳秒。转换前请检查位数。
- 10 位:很可能是 Unix 秒
- 13 位:很可能是 Unix 毫秒或 Java long 毫秒
- 16 位:很可能是微秒
- 19 位:很可能是纳秒
- 始终用单位命名列,例如 created_at_ms
为什么毫秒会转换成奇怪的日期
把毫秒值当作秒会得到极其遥远的未来日期。把秒值当作毫秒会得到接近 1970 年 1 月的日期。位数是最快的诊断方式。
- 1700000000000 作为毫秒 = 2023-11-14 22:13:20 UTC
- 1700000000000 作为秒 = 约公元 55840 年,视库而定
- 1700000000 作为秒 = 2023-11-14 22:13:20 UTC
- 1700000000 作为毫秒 = 1970-01-20 16:13:20 UTC
epoch 毫秒常见问题
A milliseconds value treated as seconds becomes an extremely far-future date. A seconds value treated as milliseconds becomes a date near January 1970. The digits are the quickest diagnostic.
- 1700000000000 as milliseconds = 2023-11-14 22:13:20 UTC
- 1700000000000 as seconds = year 55840 range, depending on the library
- 1700000000 as seconds = 2023-11-14 22:13:20 UTC
- 1700000000 as milliseconds = 1970-01-20 16:13:20 UTC
Cross-language patterns for ms-to-date
The conversion is structurally the same in every language — divide by 1000 (carefully) or use a millisecond-aware constructor. The details that bite are precision loss and naming.
- JavaScript: new Date(1700000000000) — no division needed, Date is millisecond-based
- Python: datetime.fromtimestamp(1700000000000 / 1000, tz=timezone.utc) — float division, watch for precision loss
- Java: Instant.ofEpochMilli(1700000000000L) — exact millisecond precision preserved
- PostgreSQL: SELECT to_timestamp(1700000000000.0 / 1000) — to_timestamp expects seconds, not milliseconds
- SQLite: SELECT datetime(1700000000000 / 1000, 'unixepoch') — integer division drops fractional seconds
- Go: time.UnixMilli(1700000000000) — added in Go 1.17 specifically to avoid the seconds-vs-ms mix-up
- Ruby: Time.at(0, 1700000000000, :millisecond) for exact ms precision
- C#: DateTimeOffset.FromUnixTimeMilliseconds(1700000000000L).UtcDateTime
Related articles
FAQ
- Is epoch time in ms the same as Unix timestamp?
- It uses the same epoch, but the unit is milliseconds instead of seconds. Classic Unix timestamp usually means seconds.
- How many digits is an epoch millisecond timestamp?
- For current dates, epoch milliseconds are usually 13 digits.
- Does JavaScript use milliseconds or seconds?
- JavaScript Date uses milliseconds. Date.now() returns epoch milliseconds.
- How do I convert a Java long timestamp to a date?
- If the long is epoch milliseconds, use Instant.ofEpochMilli(value); if it is epoch seconds, use Instant.ofEpochSecond(value). Check the digit count first — 13 digits is usually milliseconds, 10 digits is usually seconds.