Warum es zwei Standards gibt
Unix-Zeit wurde ursprünglich in Sekunden definiert — eine Ganzzahl wirkte natürlich für ein System, das pro Takt hochzählte. Das Date-Objekt von JavaScript wurde 1995 eingeführt und wählte Millisekunden, um Ereigniszeiten unter einer Sekunde im Browser zu unterstützen. Viele Datenbanken und Backend-Sprachen behielten Unix-Sekunden als Standard. Heute koexistieren beide Standards in jedem Code, der die JavaScript-Server-Grenze überschreitet — deshalb kann ein Wert gültig aussehen und doch ein Datum zehntausende Jahre entfernt darstellen.
Welche Einheit jede Sprache nutzt
Die sicherste Merkregel: Browser-Date-APIs wollen meist Millisekunden, Unix-artige Server-APIs liefern meist Sekunden, und neuere Zeitbibliotheken bieten oft beides. Schließe beim Lesen einer Drittanbieter-API-Doku nicht allein aus der Sprache auf die Einheit; prüfe Feldbeschreibung und Beispiele.
- 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)
Die Einheit am Wert erkennen
Eine zuverlässige Heuristik für moderne Daten: eine 10-stellige Zahl sind Sekunden; eine 13-stellige Millisekunden. Aktuelle Unix-Sekunden liegen bei rund 1,7–2,1 Milliarden (10 Stellen) und erreichen erst im Jahr 33658 13 Stellen. Aktuelle Unix-Millisekunden haben bereits 13 Stellen. Die Heuristik ist von den 2000ern bis einige tausend Jahre voraus am stärksten; für historische, negative Daten oder kompakte Test-Fixtures nutze explizite Einheiten statt zu raten.
- 10-stelliger Wert → Sekunden (z. B. 1700000000 = 2023-11-14 UTC)
- 13-stelliger Wert → Millisekunden (z. B. 1700000000000 = 2023-11-14 UTC)
- 16-stelliger Wert → Mikrosekunden (durch 1,000,000 für Sekunden teilen)
- negativer Wert → Datum vor dem 1. Januar 1970 (Sekunden oder Millisekunden)
Sichere Umwandlungsmuster
Die Umwandlung selbst ist einfache Arithmetik; das Schwierige ist die Wahl, wo sie passiert. Wandle an der Systemgrenze um, benenne den umgewandelten Wert und vermeide es, mehrdeutige rohe Zahlen durch mehrere Schichten zu reichen.
- 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
Wie Einheiten-Bugs in Produktion auftreten
Ein Sekunden-vs-Millisekunden-Bug übersteht oft die Validierung, weil beide Werte nur Zahlen sind. Er zeigt sich später als unmögliches Datum: Januar 1970 in JavaScript, wenn Sekunden als Millisekunden behandelt wurden, oder ein fernes Zukunftsjahr, wenn ein Backend Millisekunden als Sekunden behandelte.
- 1970-Datum in der JavaScript-UI → Sekunden wurden an new Date() übergeben, ohne mit 1000 zu multiplizieren
- Jahr 50000+ in Python, Go oder PHP → Millisekunden wurden an eine API gegeben, die Sekunden erwartete
- Abgelaufene Tokens, die nie ablaufen → Ablauf-Timestamp in der falschen Einheit gespeichert
- Cache-Einträge, die sofort verschwinden → Millisekunden zweimal geteilt oder Sekunden zweimal multipliziert
- Analytics-Diagramme mit leeren Bereichen → Abfragegrenzen nutzen eine andere Einheit als die gespeicherten Ereignis-Timestamps
Namenskonventionen für APIs und Datenbanken
Eine kleine Namenskonvention verhindert die meisten dieser Bugs. Veröffentliche nie ein API-Feld namens timestamp, außer die Doku ist ungewöhnlich klar. Bevorzuge Feldnamen, die Bedeutung und Einheit enthalten.
- 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
FAQ Millisekunden vs. Sekunden
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.