Unix time: guides and reference
How to get, convert and reason about Unix timestamps in the languages and tools developers use, with the pitfalls that cause off-by-hours and off-by-1000 bugs.
The current Unix timestamp in each language
A Unix timestamp is the number of seconds since 1 January 1970, 00:00:00 UTC. Most languages hand it to you in one call, but they do not agree on the unit:
| Language | Seconds | Native unit |
|---|---|---|
| JavaScript | Math.floor(Date.now() / 1000) | milliseconds |
| Python | int(time.time()) | float seconds |
| PHP | time() | seconds |
| Java | Instant.now().getEpochSecond() | seconds and nanoseconds |
| C# | DateTimeOffset.UtcNow.ToUnixTimeSeconds() | 100 ns ticks since year 1 |
| Go | time.Now().Unix() | nanoseconds |
| Rust | SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() | a Duration |
| Ruby | Time.now.to_i | seconds and nanoseconds |
| Bash | date +%s | seconds |
| PowerShell | [DateTimeOffset]::UtcNow.ToUnixTimeSeconds() | .NET ticks |
| MySQL | UNIX_TIMESTAMP() | seconds |
| PostgreSQL | extract(epoch from now())::bigint | microseconds |
| SQLite | unixepoch() | seconds |
| Excel, Sheets | =(NOW()-DATE(1970,1,1))*86400 | days since 1900 |
To convert a value by hand, paste it into the Unix timestamp converter: it works out whether the number is in seconds, milliseconds, microseconds or nanoseconds, and reads typed dates back into timestamps.
By language
Each page covers the current time, timestamp to date, date to timestamp, milliseconds, and the time zone mistakes that language makes easy.
- JavaScriptDate.now(), new Date(ts * 1000), Date.parse and the local-time traps
- Pythontime.time(), datetime.fromtimestamp with tz, and naive-datetime bugs
- PHPtime(), date() against gmdate(), strtotime() and DateTimeImmutable
- JavaInstant.now(), ofEpochSecond, ZonedDateTime and the int overflow trap
- C#DateTimeOffset.ToUnixTimeSeconds, FromUnixTimeSeconds and DateTimeKind
- Gotime.Now().Unix(), UnixMilli, time.Unix() and layout strings
- RustSystemTime::now().duration_since(UNIX_EPOCH), and chrono for dates
- RubyTime.now.to_i, Time.at(ts).utc, Time.utc and Time.iso8601
- Bash and the date commanddate +%s, date -d @ts on Linux, date -r ts on macOS
- PowerShell[DateTimeOffset]::UtcNow.ToUnixTimeSeconds() and FromUnixTimeSeconds
- SQLFROM_UNIXTIME, to_timestamp, unixepoch and DATEADD, side by side
- Excel and Google Sheets=A1/86400+DATE(1970,1,1), the reverse, and EPOCHTODATE
How Unix time works
- What is epoch time?The definition, why 1970, and what the count ignores
- Seconds, milliseconds or microseconds?Tell the unit from the digit count: 10, 13, 16 or 19
- The year 2038 problemWhy 32-bit timestamps end at 2,147,483,647 and what still uses them
- Leap seconds and Unix timeWhy a timestamp can repeat, and how clocks step or smear
- ISO 8601 or epoch time?When to send 2023-11-14T22:13:20Z and when 1700000000
- Negative timestampsHow dates before 1970 are stored, and where they break
Reference
- Notable timestampsYear starts 2020–2035, round numbers and the 32-bit limits
Three rules that prevent most timestamp bugs
- Know the unit. Ten digits is seconds, thirteen is milliseconds. Passing seconds to an API that wants milliseconds gives a date in January 1970; the reverse gives a year around 55,000.
- A timestamp has no time zone. It names one instant everywhere. Time zones only enter when you turn it into a calendar date, or parse a date that does not say which zone it is in.
- Parse dates with an explicit zone.
2023-11-14 22:13:20is a different instant in London and in Tokyo. Write2023-11-14T22:13:20Z, or tell the parser the zone, rather than relying on the server's setting.
Questions
Is a Unix timestamp always in UTC?
Yes, in the sense that it counts seconds from an instant defined in UTC, so the same number means the same moment everywhere. It carries no time zone of its own; a zone is only applied when it is displayed as a date.
How do I tell seconds from milliseconds?
Count the digits. For any date between September 2001 and November 2286, a timestamp in seconds has 10 digits, in milliseconds 13, in microseconds 16 and in nanoseconds 19.