Cosa sono i millisecondi epoch
I millisecondi epoch contano i millisecondi dal 1° gennaio 1970 alle 00:00:00 UTC. Usano lo stesso epoch Unix dei secondi Unix, ma con tre cifre di precisione in più. Un valore moderno di millisecondi epoch ha di solito 13 cifre, come 1700000000000.
Come convertire i millis in data
Se il valore è già in millisecondi, JavaScript Date può leggerlo direttamente. Se lo strumento ricevente si aspetta secondi, dividi prima per 1000. L'errore principale è moltiplicare o dividere al confine sbagliato.
- 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)
Long in data in Java e database
Le ricerche «long in data» arrivano spesso da Java, Kotlin, Android o export di database in cui un timestamp è salvato come intero a 64 bit. Il valore può essere in millisecondi epoch, ma anche in secondi, microsecondi o nanosecondi. Controlla il numero di cifre prima di convertire.
- 10 cifre: probabilmente secondi Unix
- 13 cifre: probabilmente millisecondi Unix o long Java in millisecondi
- 16 cifre: probabilmente microsecondi
- 19 cifre: probabilmente nanosecondi
- Nomina sempre le colonne con l'unità, come created_at_ms
Perché i millisecondi diventano date strane
Un valore in millisecondi trattato come secondi diventa una data molto lontana nel futuro. Un valore in secondi trattato come millisecondi diventa una data vicina al gennaio 1970. Le cifre sono la diagnosi più rapida.
- 1700000000000 come millisecondi = 2023-11-14 22:13:20 UTC
- 1700000000000 come secondi = intorno all'anno 55840, a seconda della libreria
- 1700000000 come secondi = 2023-11-14 22:13:20 UTC
- 1700000000 come millisecondi = 1970-01-20 16:13:20 UTC
FAQ sui millisecondi 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.