Software & OS

Unix Timestamp Explained: Epoch Time, Conversion and the 2038 Bug

A Unix timestamp is the number of seconds since 00:00:00 UTC on 1 January 1970, not counting leap seconds, so 1700000000 means 14 November 2023, 22:13:20 UTC. Get the current one with date +%s on Linux and macOS, [DateTimeOffset]::UtcNow.ToUnixTimeSeconds() in PowerShell, time.time() in Python or Date.now() (milliseconds) in JavaScript.

What a Unix timestamp is

The starting point, 1970-01-01 00:00:00 UTC, is called the Unix epoch. The count is a single integer with no time zone, so the same instant has the same timestamp in Tokyo and in London. That makes timestamps convenient for storing and comparing times: sorting them sorts events chronologically, and subtracting two gives the seconds between them.

POSIX, the standard behind Unix-like systems, defines the value precisely in section 4.19 "Seconds Since the Epoch" as a formula over the calendar fields, and adds that "each and every day shall be accounted for by exactly 86400 seconds". That rule is why leap seconds are left out (more on that below).

Get the current timestamp

WhereCommandUnit
Linux, macOS, Git Bashdate +%sSeconds
PowerShell (any version)[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()Seconds
Pythontime.time()Seconds, as a float
JavaScriptDate.now()Milliseconds
JavaScript, secondsMath.floor(Date.now() / 1000)Seconds

Real output, run within a few seconds of each other on 26 October 2026:

$ date +%s
1793036213

>>> time.time()
1793036213.890745

> Date.now()
1793036214028

Note the 13-digit JavaScript value. Mixing up seconds and milliseconds is the most common timestamp bug.

Convert a timestamp to a date

GNU date (Linux, Git Bash) takes an @ prefix:

$ date -u -d @0
Thu Jan  1 00:00:00 UTC 1970
$ date -u -d @1700000000
Tue Nov 14 22:13:20 UTC 2023

macOS ships BSD date, which does not accept -d @; use Python or JavaScript there. Python, with an explicit UTC zone:

>>> from datetime import datetime, timezone
>>> datetime.fromtimestamp(1700000000, tz=timezone.utc)
datetime.datetime(2023, 11, 14, 22, 13, 20, tzinfo=datetime.timezone.utc)

JavaScript's Date constructor wants milliseconds, so multiply by 1000:

$ node -p "new Date(1700000000 * 1000).toISOString()"
2023-11-14T22:13:20.000Z
$ node -p "new Date(1700000000).toISOString()"
1970-01-20T16:13:20.000Z

The second line is the classic bug: a seconds value read as milliseconds lands in January 1970. If your dates show up in 1970, this is why. PowerShell:

PS> [DateTimeOffset]::FromUnixTimeSeconds(1700000000).UtcDateTime.ToString("u")
2023-11-14 22:13:20Z

PowerShell 7.1 and later also accept Get-Date -UnixTimeSeconds 1700000000.

Convert a date to a timestamp

$ date -u -d "2026-11-19 00:00:00" +%s
1795046400

>>> int(datetime(2026, 11, 19, tzinfo=timezone.utc).timestamp())
1795046400

Leave out tz=timezone.utc and Python treats the date as local time. On a machine set to UTC+2, datetime(2026, 11, 19).timestamp() returned 1795039200.0, which is 7,200 seconds (2 hours) earlier. The same trap exists in reverse: datetime.fromtimestamp(1700000000) without a zone printed 2023-11-15 00:13:20 on that machine, the local time, with no zone attached to tell you so.

A Windows PowerShell 5.1 gotcha

Microsoft's Get-Date reference documents -UFormat %s as "Seconds elapsed since January 1, 1970 00:00:00 (UTC)". In Windows PowerShell 5.1 on a machine set to UTC+2, it returned this, alongside the correct value from DateTimeOffset:

PS> [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
1793036219
PS> Get-Date -UFormat %s
1793043419,28273

The second value is 7,200 seconds ahead (it counted from local time, not UTC), has a fractional part, and uses the regional decimal comma. Use the DateTimeOffset method in scripts that might run on 5.1.

Leap seconds: why Unix time isn't a pure count of seconds

Coordinated Universal Time (UTC) occasionally inserts a leap second to stay within 0.9 seconds of Earth's rotation. According to NIST, the last one was added on 31 December 2016, and TAI (atomic time) is now 37 seconds ahead of UTC. Because POSIX gives every day exactly 86,400 seconds, Unix time simply doesn't count leap seconds; Python's documentation says they are excluded "on all POSIX-compliant platforms". The upside is that converting a timestamp to a date is pure arithmetic, with no table of leap seconds needed. For the difference between UTC and the older GMT, see UTC vs GMT.

The year 2038 problem

A signed 32-bit integer can hold at most 2,147,483,647. As a timestamp that is:

$ date -u -d @2147483647
Tue Jan 19 03:14:07 UTC 2038

One second later, a 32-bit signed counter wraps around to -2,147,483,648, which decodes to a date in 1901:

$ date -u -d @-2147483648
Fri Dec 13 20:45:52 UTC 1901

Python's docs note that the cut-off for its time functions "is determined by the C library; for 32-bit systems, it is typically in 2038". Code that stores time in a 64-bit integer is not affected. The risk sits wherever a 32-bit field survives: older embedded devices, binary file formats and database columns. If you design a schema today, store timestamps in a 64-bit integer column or a native timestamp type.

Seconds, milliseconds, microseconds: how to tell them apart

Digits (for dates in 2026)UnitExample
10Seconds1793036213
13Milliseconds1793036214028
16Microseconds1793036213890745

To format a converted date for display, Python's format codes are listed in the strftime cheat sheet.

Frequently asked questions

Is a Unix timestamp always in UTC?
Yes. A Unix timestamp counts seconds from 1970-01-01 00:00:00 UTC and has no time zone of its own. The same instant has the same timestamp everywhere in the world. Time zones only come in when you convert the number into a calendar date and clock time for display.
How do I tell if a timestamp is in seconds or milliseconds?
Count the digits. A current timestamp in seconds has 10 digits (for example 1793036213, in October 2026), and one in milliseconds has 13 (1793036214028). JavaScript's Date.now() returns milliseconds, while date +%s, Python's time.time() and PowerShell's ToUnixTimeSeconds() return seconds.
Do Unix timestamps include leap seconds?
No. POSIX defines every day as exactly 86,400 seconds, and Python's documentation says leap seconds are excluded on all POSIX-compliant platforms. So a Unix timestamp is not a true count of elapsed SI seconds since 1970; it is a calendar-friendly count that skips every leap second inserted into UTC.
What is timestamp 0?
Timestamp 0 is Thursday, 1 January 1970, 00:00:00 UTC, called the Unix epoch. Negative timestamps represent instants before 1970, although some functions and platforms don't accept them.

Sources