Unix Timestamps: Understanding Computer Time

Unix Timestamps: Understanding Computer Time

Every programmer encounters Unix timestamps eventually. That mysterious number like 1702339200 actually represents a specific moment in time. Understanding timestamps unlocks debugging power and cross-system compatibility.

What Is a Unix Timestamp?

A Unix timestamp is the number of seconds since January 1, 1970, 00:00:00 UTC—a moment called the Unix Epoch. This single number represents any moment in time.

Examples:

  • 0 = January 1, 1970 00:00:00 UTC
  • 1000000000 = September 9, 2001 01:46:40 UTC
  • 1702339200 = December 11, 2023 00:00:00 UTC

Convert timestamps with our timestamp converter.

Why Use Timestamps?

Universal Format

Unlike "December 11, 2023" or "11/12/2023" (US vs. UK confusion), timestamps are unambiguous worldwide.

Easy Calculations

Time differences are simple subtraction:

duration = endTimestamp - startTimestamp;

No Time Zone Confusion

Timestamps are always UTC. Local time is a display concern, not storage.

Compact Storage

A 32-bit integer stores any date. Much smaller than date strings.

Sorting

Timestamps sort chronologically as numbers. No date parsing needed.

Reading Timestamps

Quick mental math for rough estimates:

  • 1 day = 86,400 seconds
  • 1 week = 604,800 seconds
  • 1 year ≈ 31,536,000 seconds

Current timestamps (2020s) are around 1.7 billion.

Common Timestamp Formats

Seconds (Standard Unix)

1702339200

10 digits, the traditional format.

Milliseconds (JavaScript)

1702339200000

13 digits. JavaScript, Java, and others use this.

Microseconds

1702339200000000

16 digits. High-precision applications.

Programming with Timestamps

JavaScript

// Current timestamp (milliseconds)
Date.now();

// Convert to timestamp
new Date("2023-12-11").getTime();

// Convert from timestamp
new Date(1702339200000);

Python

import time
from datetime import datetime

# Current timestamp
time.time()

# Convert to timestamp
datetime(2023, 12, 11).timestamp()

# Convert from timestamp
datetime.fromtimestamp(1702339200)

PHP

// Current timestamp
time();

// Convert to timestamp
strtotime("2023-12-11");

// Convert from timestamp
date("Y-m-d", 1702339200);

The Year 2038 Problem

32-bit signed integers can only represent timestamps up to January 19, 2038 03:14:07 UTC. After that, they overflow to negative numbers.

Solutions:

  • Use 64-bit integers (most modern systems)
  • Store in larger data types
  • Most programming languages have already migrated

This is less urgent than Y2K was, but legacy systems need attention.

Timestamps and Time Zones

Timestamps are always UTC. Convert to local time for display:

// JavaScript
const date = new Date(1702339200000);
date.toLocaleString(); // Uses browser's locale
date.toLocaleString("en-US", { timeZone: "America/New_York" });

Store as UTC, display as local. This prevents time zone bugs.

Database Timestamp Types

MySQL

  • TIMESTAMP: 4 bytes, 1970-2038, auto-converts to UTC
  • DATETIME: 8 bytes, wider range, no conversion

PostgreSQL

  • timestamp: No time zone info
  • timestamptz: Time zone aware (recommended)

Common Timestamp Tasks

Get Start of Day

// JavaScript
const startOfDay = new Date();
startOfDay.setHours(0, 0, 0, 0);
startOfDay.getTime();

Add Time

// Add 7 days
const newTimestamp = timestamp + (7 * 24 * 60 * 60);

Compare Dates

if (timestamp1 > timestamp2) {
    // timestamp1 is later
}

Tools

Our timestamp converter handles both seconds and milliseconds, making debugging quick and easy.

Try Timestamp Converter Now