什麼是 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.