epoch ミリ秒とは
epoch ミリ秒は、1970 年 1 月 1 日 00:00:00 UTC からのミリ秒を数えます。Unix 秒と同じ Unix エポックを使いますが、精度が 3 桁多くなります。現代の epoch ミリ秒の値は通常 13 桁で、1700000000000 のようになります。
ミリ秒を日付に変換する方法
値がすでにミリ秒なら、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 から日付」の検索は、タイムスタンプを 64 ビット整数で保存する Java・Kotlin・Android・データベースのエクスポートから来ることが多いです。値は 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 ミリ秒 FAQ
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.