Introduction

Unix timestamps (also called epoch time) are a standard way to represent time in computer systems. They’re simple, universal, and timezone-independent, making them ideal for APIs, databases, and logging.

What is a Unix Timestamp?

A Unix timestamp is the number of seconds (or milliseconds) that have elapsed since January 1, 1970, 00:00:00 UTC (the Unix epoch).

Examples

Current time: 2024-12-07 10:30:00 UTC
Unix timestamp: 1733568600 (seconds)
Unix timestamp: 1733568600000 (milliseconds)

Why Use Unix Timestamps?

Advantages:

  • ✅ Timezone-independent
  • ✅ Easy to compare
  • ✅ Simple storage (single number)
  • ✅ Universal format
  • ✅ Precise (down to milliseconds)

Disadvantages:

  • ❌ Not human-readable
  • ❌ Year 2038 problem (32-bit)
  • ❌ No timezone information

Conversion Examples

JavaScript

// Current timestamp
const now = Math.floor(Date.now() / 1000); // Seconds
const nowMs = Date.now(); // Milliseconds

// Timestamp to Date
const timestamp = 1733568600;
const date = new Date(timestamp * 1000);
console.log(date.toISOString()); // "2024-12-07T10:30:00.000Z"

// Date to timestamp
const dateObj = new Date("2024-12-07");
const ts = Math.floor(dateObj.getTime() / 1000);

Python

import time
from datetime import datetime

# Current timestamp
now = int(time.time())  # Seconds

# Timestamp to datetime
timestamp = 1733568600
dt = datetime.fromtimestamp(timestamp)

# Datetime to timestamp
dt = datetime(2024, 12, 7)
ts = int(dt.timestamp())

Time Zones

UTC vs Local Time

// UTC timestamp
const utcTimestamp = 1733568600;

// Convert to local time
const localDate = new Date(utcTimestamp * 1000);
console.log(localDate.toString()); // Local timezone

// Convert to UTC
const utcDate = new Date(utcTimestamp * 1000).toISOString();
console.log(utcDate); // Always UTC

Common Use Cases

API Responses

{
  "id": 1,
  "name": "Alice",
  "createdAt": 1733568600
}

Database Storage

CREATE TABLE users (
  id INT,
  name VARCHAR(100),
  created_at BIGINT  -- Unix timestamp
);

Logging

[1733568600] INFO: User logged in
[1733568610] ERROR: Database connection failed

Tools

Use our tools:

Conclusion

Unix timestamps are essential for:

Use cases:

  • API responses
  • Database storage
  • Logging
  • Time calculations
  • Cross-system communication

Remember:

  • Always store in UTC
  • Convert for display
  • Use milliseconds for precision
  • Handle timezone conversions

Next Steps