なぜ 2 つの標準があるのか
Unix 時間は当初、秒で定義されました。クロックのティックごとに 1 進むシステムには整数が自然でした。JavaScript の Date オブジェクトは 1995 年に導入され、ブラウザでの秒未満のイベント計測のためにミリ秒を選びました。多くのデータベースやバックエンド言語は Unix 秒を既定のまま保ちました。今では両方の標準が JavaScript とサーバーの境界をまたぐすべてのコードに共存し、だからこそ値が一見正しく見えても、数万年先の日付を表していることがあります。
言語ごとに使う単位
分かれ方を覚える最も安全な方法:ブラウザの Date API は通常ミリ秒を求め、Unix 系のサーバー API は通常秒を返し、新しい時間ライブラリは両方を提供することが多い、というものです。サードパーティ API のドキュメントを読むとき、言語だけから単位を推測しないでください。フィールドの説明と例を確認します。
- Milliseconds: JavaScript Date.now(), Java System.currentTimeMillis(), Java Instant.toEpochMilli(), .NET ToUnixTimeMilliseconds()
- Seconds: Python time.time() (float), PHP time(), Go time.Now().Unix(), Ruby Time.now.to_i, C time(NULL), PostgreSQL EXTRACT(EPOCH)
- Both: Rust — duration.as_secs() for seconds, duration.as_millis() for milliseconds
- Python note: time.time() returns a float, so milliseconds are available as int(time.time() * 1000)
値から単位を自動判別する
現代の日付に対する信頼できる経験則:10 桁の数値は秒、13 桁はミリ秒です。現在の Unix 秒は約 17〜21 億(10 桁)で、13 桁に達するのは西暦 33658 年です。現在の Unix ミリ秒はすでに 13 桁です。この経験則は 2000 年代から数千年先までで最も強力です。歴史的な日付・負の日付・短いテストフィクスチャでは、推測せず明示的な単位を使ってください。
- 10 桁の値 → 秒(例:1700000000 = 2023-11-14 UTC)
- 13 桁の値 → ミリ秒(例:1700000000000 = 2023-11-14 UTC)
- 16 桁の値 → マイクロ秒(秒にするには 1,000,000 で割る)
- 負の値 → 1970 年 1 月 1 日より前の日付(秒またはミリ秒)
安全な変換パターン
変換自体は単純な算術です。難しいのは、どこで変換するかを選ぶことです。システムの境界で変換し、変換後の値に名前を付け、曖昧な生の数値を複数の層に通さないようにします。
- Seconds to milliseconds: seconds * 1000
- Milliseconds to whole seconds — JavaScript: Math.floor(ms / 1000)
- Milliseconds to whole seconds — Python: ms // 1000
- Milliseconds to whole seconds — Go: ms / 1000 (integer division)
- Universal guard in JavaScript: const toMs = ts => ts < 1e11 ? ts * 1000 : ts
単位のバグが本番でどう現れるか
秒対ミリ秒のバグは、どちらの値もただの数値なので、検証をすり抜けがちです。後になって、ありえない日付として現れます:秒をミリ秒として扱うと JavaScript で 1970 年 1 月に、ミリ秒を秒としてバックエンドが扱うと遠い未来の年になります。
- JavaScript の UI で 1970 年の日付 → 1000 を掛けずに秒を new Date() に渡した
- Python・Go・PHP で 50000 年以降 → 秒を期待する API にミリ秒を渡した
- 失効しない期限切れトークン → 失効タイムスタンプを誤った単位で保存した
- すぐ消えるキャッシュエントリ → ミリ秒を二重に割った、または秒を二重に掛けた
- 範囲が空の解析チャート → クエリの境界が保存済みイベントのタイムスタンプと異なる単位を使っている
API とデータベースの命名規則
小さな命名規則がこれらのバグの大半を防ぎます。ドキュメントが特に明確でない限り、timestamp という名前の API フィールドを公開しないでください。意味と単位の両方を含むフィールド名を優先します。
- createdAtMs — Unix milliseconds, best for JavaScript clients
- createdAtSeconds — Unix seconds, common for backend services
- createdAtIso — ISO 8601 string, useful for human-readable API responses
- expiresAtUnixSeconds — explicit enough for auth tokens and signed URLs
- event_time TIMESTAMPTZ — native database time, with conversion handled by the database
ミリ秒 vs 秒 FAQ
A tiny naming convention prevents most of these bugs. Never publish an API field called timestamp unless the documentation is unusually clear. Prefer field names that include both meaning and unit.
- createdAtMs — Unix milliseconds, best for JavaScript clients
- createdAtSeconds — Unix seconds, common for backend services
- createdAtIso — ISO 8601 string, useful for human-readable API responses
- expiresAtUnixSeconds — explicit enough for auth tokens and signed URLs
- event_time TIMESTAMPTZ — native database time, with conversion handled by the database
Microseconds (16 digits) and nanoseconds (19 digits)
Most production timestamps are seconds (10 digits) or milliseconds (13 digits), but two higher-precision formats also appear in real systems — microseconds and nanoseconds. The same digit-counting rule extends.
- 10 digits ≈ Unix seconds (~1.7×10⁹ today)
- 13 digits ≈ Unix milliseconds (~1.7×10¹²)
- 16 digits ≈ Unix microseconds (~1.7×10¹⁵) — Python time.time_ns()/1000, Postgres TIMESTAMP precision 6
- 19 digits ≈ Unix nanoseconds (~1.7×10¹⁸) — Java Instant.toEpochNano(), Go time.UnixNano(), Temporal.Instant.epochNanoseconds
- PostgreSQL to_timestamp() takes seconds with an optional fractional part; passing milliseconds yields a date around the year 55000
- Convert from any precision to milliseconds via integer arithmetic before doing further math
- Silent truncation: if a database column has millisecond precision but you store nanoseconds, the bottom 6 digits are dropped on insert
How many microseconds in a second? 1 second to milliseconds, microseconds to milliseconds
A quick reference for the unit math that drives every seconds/milliseconds/microseconds bug. The questions "how many microseconds in a second", "1 second to milliseconds", and "microseconds to milliseconds" are all powers of 1000 — but every order of magnitude is one place where production code goes wrong. Keep the table below within reach when writing high-resolution timestamp code.
- 1 second to milliseconds = 1,000 ms (10³)
- How many microseconds in a second? = 1,000,000 µs (10⁶)
- 1 second to nanoseconds = 1,000,000,000 ns (10⁹)
- Microseconds to milliseconds = µs ÷ 1,000 (10³ step down)
- Milliseconds to microseconds = ms × 1,000
- Nanoseconds to microseconds = ns ÷ 1,000
- Common precision: JavaScript Date.now() = ms, performance.now() = sub-ms float, Go time.UnixNano() = ns, Postgres TIMESTAMP precision 6 = µs
- Bug pattern: an API returns µs, code divides by 1000 once expecting seconds — actually got ms; result is 1000× off
Millisecond timer, time to ms, milliseconds to seconds conversion
Quick reference for the unit phrasings: "time to ms" (any time value scaled into milliseconds), "milliseconds to seconds conversion" (÷1000), and "millisecond timer" (a high-resolution timer for short intervals). For measuring elapsed time prefer a monotonic clock — wall-clock time can jump backwards on NTP correction or DST.
- Time to ms / time to milliseconds: seconds × 1000 = ms; microseconds ÷ 1000 = ms; nanoseconds ÷ 1,000,000 = ms
- Milliseconds to seconds conversion: ms ÷ 1000 = seconds; Math.floor(ms/1000) for integer seconds
- Millisecond timer (JavaScript): performance.now() returns sub-ms float since page load; Date.now() returns wall-clock ms
- Millisecond timer (Python): time.perf_counter() (monotonic float seconds); time.monotonic_ns() returns ns
- Millisecond timer (Java): System.nanoTime() for monotonic ns; Instant.now() for wall-clock ns precision
- Stopwatch rule: use monotonic clocks for elapsed time; wall-clock can jump backwards on NTP or DST
FAQ
- Is a 13-digit timestamp always milliseconds?
- For modern real-world Unix timestamps, yes: 13 digits usually means milliseconds. Very far-future seconds timestamps can also reach 13 digits, so critical systems should still carry explicit unit metadata.
- Should I store seconds or milliseconds?
- Store the unit your system naturally uses, but document it and keep it consistent. JavaScript-heavy systems often use milliseconds; Unix-style backends and many databases commonly use seconds or native datetime columns.
- Why use Math.floor(ms / 1000) instead of ms / 1000?
- Unix seconds are usually whole seconds. Math.floor removes the fractional part so APIs expecting integer seconds do not receive a decimal value.
- How do I convert milliseconds to seconds?
- Divide by 1000 and drop the fraction: Math.floor(ms / 1000) in JavaScript, ms // 1000 in Python, or ms / 1000 with integer division in Go. To go the other way, multiply seconds by 1000.