DEV Community

Anup Karanjkar
Anup Karanjkar

Posted on Originally published at wowhow.cloud

Epoch Time and Unix Timestamps Explained With Examples

Epoch time is the number of seconds that have elapsed since 00:00:00 UTC on January 1, 1970, the Unix Epoch, with leap seconds ignored.[1][2] It is one integer, it has no timezone, and it sorts correctly. A Unix timestamp is the same thing under another name: 1700000000 is 2023-11-14 22:13:20 UTC. This guide gives worked conversions, a reference table for seconds, milliseconds, microseconds and nanoseconds, code for five environments, and the edge cases that cause real bugs. To convert a value right now, paste it into the free timestamp converter.

What epoch time means

POSIX defines the Epoch as zero hours, zero minutes, zero seconds on January 1, 1970 UTC.[2] "Seconds since the Epoch" is then a value that approximates how many seconds have passed since that instant.[1] Two details in that definition matter more than the headline.

First, the word is "approximates." The POSIX text says the relationship between the real UTC time and the system's seconds-since-Epoch value is unspecified, and that each and every day is accounted for by exactly 86400 seconds.[1] Second, the standard gives a formula that turns a calendar date into that integer using only days, hours, minutes and seconds. There is no leap second term in it.

The Linux time(2) man page says the same thing from the implementation side: the value is not the actual number of seconds between a time and the Epoch because of leap seconds, and Linux systems normally follow the POSIX requirement that the value ignore leap seconds.[3] Python's docs agree: leap seconds are excluded on all POSIX-compliant platforms.[4]

Worked examples

Because every day is exactly 86,400 seconds, you can do the arithmetic by hand. One day after the Epoch is 86400, which is 1970-01-02 00:00:00 UTC. A billion seconds is the more famous milestone: 1000000000 is 2001-09-09 01:46:40 UTC.

The table below lists values converted with GNU date -u -d @N (output from running it, GNU coreutils 9.4). Bookmark it for sanity checks.

Epoch seconds UTC date and time Why it matters

| 0 | 1970-01-01 00:00:00 | The Epoch itself |

| 86400 | 1970-01-02 00:00:00 | Exactly one POSIX day |

| 1000000000 | 2001-09-09 01:46:40 | One billion seconds |

| 1234567890 | 2009-02-13 23:31:30 | The sequence 1234567890 |

| 1700000000 | 2023-11-14 22:13:20 | Handy test value |

| 1795392000 | 2026-11-23 00:00:00 | Midnight UTC on the day this was written |

| 2147483647 | 2038-01-19 03:14:07 | Largest signed 32-bit value (2^31 - 1) |

| 2147483648 | 2038-01-19 03:14:08 | First second a signed 32-bit integer cannot hold |

| 4294967295 | 2106-02-07 06:28:15 | Largest unsigned 32-bit value (2^32 - 1) |

Reading a timestamp by eye is a skill worth having. As a landmark, 1700000000 is November 2023 and 1795392000 is the start of November 23, 2026, so a current timestamp in seconds sits just under 1.8 billion. If the number you are looking at has 13 digits, it is not seconds.

Seconds, milliseconds, microseconds and nanoseconds

The most common epoch bug is a unit mismatch. The Unix convention counts seconds, but JavaScript counts milliseconds, Python can return nanoseconds, and Go exposes all four. Output from running Date.now() in Node, time.time_ns() in Python and time.Now() in Go on the same machine showed the digit counts below.

Unit Digits today Example (2023-11-14 22:13:20 UTC) Where you meet it

| Seconds | 10 | 1700000000 | POSIX time(), date +%s, Go Unix() |

| Milliseconds | 13 | 1700000000000 | JavaScript Date.now(), Go UnixMilli() |

| Microseconds | 16 | 1700000000000000 | Go UnixMicro() |

| Nanoseconds | 19 | 1700000000000000000 | Python time_ns(), Go UnixNano() |

Each step is a factor of 1,000. MDN documents that Date.now() returns milliseconds elapsed since the epoch, which it defines as midnight at the start of January 1, 1970 UTC.[5] Python's time.time() returns seconds as a float, and time.time_ns() returns an integer number of nanoseconds; the docs recommend the latter to avoid the precision loss of a float.[4]

The failure looks like this. Feed 1700000000 (seconds) to JavaScript's new Date(), which expects milliseconds, and node printed 1970-01-20T16:13:20.000Z. Dates that land in January 1970 almost always mean a seconds value went into a milliseconds slot.

Getting the current epoch time in code

Each snippet below was run, and each printed a 10-digit seconds value (13 digits for milliseconds) on the machine used for this post. Start with the shell, where GNU date documents %s as seconds since the Epoch, with leap seconds not counted unless leap second support is available.[9]

# current epoch seconds
date +%s

# epoch seconds to a UTC date
date -u -d @1700000000
# Tue Nov 14 22:13:20 UTC 2023

# epoch milliseconds to a UTC date: divide first
date -u -d @$((1700000000000/1000))
Enter fullscreen mode Exit fullscreen mode

In Python, time.time() returns a float, so cast when you want an integer. Pass tz=timezone.utc to datetime.fromtimestamp so the result is explicitly UTC.

import time
from datetime import datetime, timezone

now_s = int(time.time())          # seconds
now_ns = time.time_ns()           # integer nanoseconds

print(datetime.fromtimestamp(1700000000, tz=timezone.utc).isoformat())
# 2023-11-14T22:13:20+00:00
Enter fullscreen mode Exit fullscreen mode

In JavaScript, Date.now() is milliseconds. Divide and floor for seconds, multiply by 1000 on the way back in.

const nowMs = Date.now();
const nowSec = Math.floor(nowMs / 1000);

console.log(new Date(1700000000 * 1000).toISOString());
// 2023-11-14T22:13:20.000Z

console.log(Math.floor(new Date("2038-01-19T03:14:07Z").getTime() / 1000));
// 2147483647
Enter fullscreen mode Exit fullscreen mode

Go has separate methods for each unit, and Go's docs state that Time.Unix() returns seconds since January 1, 1970 UTC, independent of the time's location.[7]

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    fmt.Println(now.Unix(), now.UnixMilli())

    fmt.Println(time.Unix(1700000000, 0).UTC())
    // 2023-11-14 22:13:20 +0000 UTC
}
Enter fullscreen mode Exit fullscreen mode

PostgreSQL documents EXTRACT(EPOCH FROM ...) for timestamp with time zone values as the number of seconds since 1970-01-01 00:00:00 UTC, negative for earlier timestamps, and to_timestamp converts back.[8] The docs' own example: SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08'); returns 982384720.120000, and SELECT to_timestamp(982384720.12); returns 2001-02-17 04:38:40.12+00. Those are the documented results, not a run on our side.

Unix timestamp vs epoch time

They are the same thing. "Unix timestamp," "Unix time," "POSIX time" and "epoch time" all point at the same count of seconds from the same 1970 origin. Python's docs say exactly that about leap seconds not being counted: this is commonly referred to as Unix time.[4] The only real differences are habit and unit. When a developer says "Unix timestamp" they usually mean seconds. When they say "epoch" they might mean seconds, milliseconds or nanoseconds, so confirm the unit before you build anything on it.

Python's docs also note that the functions in the time module may not handle dates before the epoch or far in the future, with the future cut-off determined by the C library.[4] That brings us to the edge cases.

Edge cases that break real systems

Leap seconds are not in the count

UTC occasionally inserts a leap second. Epoch time does not count it. Under POSIX, each day is accounted for by exactly 86400 seconds, so a leap second has no distinct value of its own.[1] Practical consequences: you can compute elapsed days with plain division by 86400, and you cannot use epoch differences to measure physical elapsed time to the second across a leap second boundary. For almost every application that is the right trade. For astronomy or metrology, use a time scale that counts them.

Negative values and dates before 1970

POSIX says that if the year is before 1970 or the value is negative, the relationship between the value and a UTC name is undefined.[1] In practice, plenty of software accepts negatives. GNU date printed 1969-12-31 23:59:59 UTC for -1 when run for this post, and PostgreSQL documents negative epoch values for earlier timestamps.[8] The catch: the standard does not promise it, and Python warns that its time functions may not handle dates before the epoch.[4] If you store birthdates or historical records, test negatives on every platform you ship to, or store ISO 8601 dates instead.

The 2038 problem

A signed 32-bit integer tops out at 2147483647, which is 2038-01-19 03:14:07 UTC. One second later the value does not fit. Python's docs put it plainly: on 32-bit systems the cut-off in the future is typically in 2038.[4] Go's docs make the contrast explicit: Unix-like operating systems often record time as a 32-bit count of seconds, but Go's Unix() returns a 64-bit value that is valid for billions of years into the past or future.[7] An unsigned 32-bit field buys you until 2106-02-07 06:28:15 UTC (4294967295), at the cost of losing negative values.

The fix is a 64-bit time_t, or a 64-bit column in your database. Audit file formats, protocol fields and old schemas for INT or int32 timestamp columns. Those survive forever in places nobody looks.

JavaScript has its own range

A JavaScript Date holds at most plus or minus 8,640,000,000,000,000 milliseconds from the epoch, which MDN gives as April 20, 271822 BC to September 13, 275760 AD.[6] Running new Date(8.64e15 + 1).getTime() in Node returned NaN. Beyond that limit you get an invalid date, not an overflow wrap.

Go nanoseconds run out in 2262

Go's UnixNano() returns an int64, and its docs say the result is undefined for dates before the year 1678 or after 2262.[7] That is a long way off, but it also means the zero time.Time value gives an undefined result from UnixNano(), which does trip people.

Timezones: epoch has none

An epoch value is the same everywhere on Earth at the same instant. Timezones only appear when you format it. Output from running TZ=America/New_York date -d @0 printed 1969-12-31 19:00:00 EST, while TZ=Asia/Kolkata date -d @0 printed 1970-01-01 05:30:00 IST. Same integer, three clock faces. The bug pattern is converting a local wall-clock string to epoch with the wrong zone, so store the epoch (or a UTC timestamp) and convert to local time only for display. If your data lives in PostgreSQL, our timestamp vs timestamptz guide covers the same trap on the database side. PostgreSQL's own docs warn that applying to_timestamp to an epoch extracted from a date or timestamp value (no time zone) effectively assumes the original value was UTC.[8]

Common mistakes

These are the ones that show up again and again.

Mixing units. Count digits. Ten means seconds, thirteen milliseconds, sixteen microseconds, nineteen nanoseconds. A small function makes the check explicit:

def unit_of(ts: int) -> str:
    ts = abs(ts)
    if ts >= 10**17: return "nanoseconds"
    if ts >= 10**14: return "microseconds"
    if ts >= 10**11: return "milliseconds"
    return "seconds"

for v in (1700000000, 1700000000000, 1700000000000000, 1700000000000000000):
    print(v, unit_of(v))
# 1700000000 seconds
# 1700000000000 milliseconds
# 1700000000000000 microseconds
# 1700000000000000000 nanoseconds
Enter fullscreen mode Exit fullscreen mode

That heuristic is ours, not a standard. It treats anything under 100 billion as seconds, so a millisecond value from before 1973-03-03 is misread as seconds. It is a debugging aid, not something to put in a parser that handles historical data.

Using floats for nanoseconds. Python's docs point to time_ns() to avoid the precision loss of the float type.[4] JavaScript has a related limit: MDN gives Number.MAX_SAFE_INTEGER as 9,007,199,254,740,991, and a 19-digit nanosecond epoch such as 1700000000000000000 is larger than that, so carry it as a string or a BigInt.[6]

Parsing a local string as UTC. A date string without an offset is ambiguous. Attach the zone before converting, or send ISO 8601 with a Z.

Assuming monotonic. Wall-clock epoch time can jump when a system clock is adjusted; the POSIX text leaves how changes align the value with real time implementation-defined.[1] For measuring durations inside one process, use the monotonic clock your language provides, not the difference of two epoch reads.

Convert an epoch value without writing code

For one-off checks, a converter beats a REPL. Paste a 10- or 13-digit value into the timestamp converter and read off the UTC and local time, or enter a date to get the epoch back. Use the magnitude table above to decide which unit you are holding before you trust the answer.

Quick reference

Epoch time: seconds since 1970-01-01 00:00:00 UTC, every day exactly 86400 seconds, leap seconds ignored. Ten digits means seconds. Thirteen means milliseconds. Values below zero and dates before 1970 are undefined in POSIX even though most tools handle them. Signed 32-bit storage ends at 2038-01-19 03:14:07 UTC. Store the integer in UTC, convert to a zone only when you display it.

Sources

  1. 4.19 Seconds Since the Epoch, POSIX Base Definitions, The Open Group (accessed November 23, 2026)

  2. 3.125 Epoch, POSIX Definitions, The Open Group (accessed November 23, 2026)

  3. time(2), Linux manual page, man7.org (accessed November 23, 2026)

  4. time: Time access and conversions, Python documentation (accessed November 23, 2026)

  5. Date.now(), MDN Web Docs (accessed November 23, 2026)

  6. Date, MDN Web Docs (accessed November 23, 2026)

  7. time package, Go Packages (accessed November 23, 2026)

  8. 9.9. Date/Time Functions and Operators, PostgreSQL Documentation (accessed November 23, 2026)

9. Time conversion specifiers, GNU Coreutils manual (accessed November 23, 2026)

Originally published at wowhow.cloud

Top comments (0)