TimestampsUnixBeginner

Understanding Unix Timestamps

March 4, 2026·4 min read

Unix timestamps appear everywhere: logs, databases, message queues, analytics pipelines, and API payloads. They are compact and portable, but they also hide timezone assumptions that cause expensive debugging sessions. This article explains how timestamps work and how to handle them with fewer surprises.

1. What Is a Unix Timestamp?

A Unix timestamp (also called "epoch time" or "POSIX time") is the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC — the "Unix Epoch." It is a universal, timezone-independent way to represent a point in time as a single integer.

Unix Epoch: 0 → 1970-01-01 00:00:00 UTC

Y2K: 946684800 → 2000-01-01 00:00:00 UTC

Now (approx): 1741824000 → 2026-03-13 00:00:00 UTC

Year 2038 problem: 2147483647 → 2038-01-19 03:14:07 UTC

2. Seconds vs Milliseconds

One of the most frequent bugs in timestamp handling is confusing seconds-based timestamps with milliseconds-based timestamps:

FormatExampleUsed By
Seconds (Unix)1741824000Unix, Linux, Python time.time(), most databases, JWT exp
Milliseconds1741824000000JavaScript Date.now(), Java System.currentTimeMillis()
Microseconds1741824000000000Python datetime, PostgreSQL TIMESTAMP
Nanoseconds1741824000000000000Go time.Now().UnixNano(), OpenTelemetry

Quick sanity check: a 10-digit number is seconds, 13-digit is milliseconds. If your year comes out as 2525, you likely passed milliseconds where seconds were expected.

3. Converting Timestamps in Code

JavaScript

// Current timestamp
const nowMs = Date.now();               // milliseconds
const nowSec = Math.floor(Date.now() / 1000); // seconds

// Timestamp → Date string
const date = new Date(1741824000 * 1000);
console.log(date.toISOString()); // "2026-03-13T00:00:00.000Z"
console.log(date.toLocaleString('en-US', { timeZone: 'America/New_York' }));

// Date string → Timestamp
const ts = Math.floor(new Date('2026-03-13T00:00:00Z').getTime() / 1000);
console.log(ts); // 1741824000

Python

import time
from datetime import datetime, timezone

# Current timestamp (seconds)
now = int(time.time())  # 1741824000

# Timestamp → datetime (UTC)
dt = datetime.fromtimestamp(1741824000, tz=timezone.utc)
print(dt.isoformat())  # 2026-03-13T00:00:00+00:00

# datetime → timestamp
ts = int(datetime(2026, 3, 13, tzinfo=timezone.utc).timestamp())
print(ts)  # 1741824000

4. Implementation: Quick Conversion Helpers

// JS: Detect seconds vs milliseconds (10 vs 13 digits)
function toDate(ts) {
  const ms = ts < 1e12 ? ts * 1000 : ts;
  return new Date(ms);
}

// Current timestamp (seconds)
const now = Math.floor(Date.now() / 1000);

// Format for display (locale-aware)
toDate(1741824000).toLocaleString('en-US', { timeZone: 'UTC' });

5. Timezone Pitfalls

⚠️ Always store in UTC

Never store timestamps in local time. Always use UTC in databases and APIs. Convert to local time only for display.

⚠️ Use timezone-aware datetime objects

In Python, naive datetime objects have no timezone info and can cause silent bugs. Always use tz=timezone.utc or pytz.

⚠️ The Year 2038 Problem

32-bit signed integers can only store timestamps up to 2038-01-19. Ensure your database and language use 64-bit integers for timestamps.

⚠️ DST (Daylight Saving Time)

When converting to local time, always use a proper timezone library (pytz, Luxon, date-fns-tz) — never manually add/subtract hours.

6. Conclusion

Unix timestamps are the universal language of time in software. Store in UTC, handle precision carefully (seconds vs milliseconds), use timezone-aware libraries for local-time conversion, and future-proof with 64-bit integers. Use our Timestamp Converter to instantly decode any epoch value.