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:

LanguageSecondsNative unit
JavaScriptMath.floor(Date.now() / 1000)milliseconds
Pythonint(time.time())float seconds
PHPtime()seconds
JavaInstant.now().getEpochSecond()seconds and nanoseconds
C#DateTimeOffset.UtcNow.ToUnixTimeSeconds()100 ns ticks since year 1
Gotime.Now().Unix()nanoseconds
RustSystemTime::now().duration_since(UNIX_EPOCH)?.as_secs()a Duration
RubyTime.now.to_iseconds and nanoseconds
Bashdate +%sseconds
PowerShell[DateTimeOffset]::UtcNow.ToUnixTimeSeconds().NET ticks
MySQLUNIX_TIMESTAMP()seconds
PostgreSQLextract(epoch from now())::bigintmicroseconds
SQLiteunixepoch()seconds
Excel, Sheets=(NOW()-DATE(1970,1,1))*86400days 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

Reference

Three rules that prevent most timestamp bugs

  1. 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.
  2. 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.
  3. Parse dates with an explicit zone. 2023-11-14 22:13:20 is a different instant in London and in Tokyo. Write 2023-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.