Every developer has encountered this scenario: you're debugging a log file, checking an API response, or querying a database, and you see a number like 1719792000. What does it mean? That number is a Unix timestamp—an epoch time representing the number of seconds since January 1, 1970.

Understanding how to convert between human-readable timestamps and epoch time is essential for modern software development. Whether you're working with JavaScript's Date.now(), Python's time.time(), or Unix command-line tools, epoch timestamps are everywhere.

This guide explains what epoch time is, why it matters, and how to convert timestamps to epoch (and back) using various programming languages and online tools.

What Is Epoch Time?

Epoch time, also known as Unix time or POSIX time, is a system for describing a point in time. It's defined as the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC—the Unix epoch.

For example:

  • Epoch 0 = January 1, 1970, 00:00:00 UTC
  • Epoch 1719792000 = July 1, 2024, 00:00:00 UTC
  • Epoch 1784000000 = July 11, 2026 (approximately)

The beauty of epoch time is its simplicity: it's just a single integer. No time zones, no daylight saving time complications, no calendar quirks. Just count the seconds from a fixed starting point.

Why Use Epoch Timestamps?

Epoch timestamps solve several problems that arise when working with dates and times:

1. Time Zone Independence

A timestamp like 2024-07-01 12:00:00 is ambiguous without a time zone. Is that noon in New York? London? Tokyo? Each interpretation represents a different moment in time.

An epoch timestamp like 1719835200 is unambiguous. It represents one specific moment, regardless of where you are in the world.

2. Easy Comparison and Sorting

Comparing epoch timestamps is trivial: larger numbers are later in time. This makes sorting, filtering, and range queries straightforward:

-- Find all records created after July 1, 2024
SELECT * FROM events WHERE created_at > 1719792000;

3. Compact Storage

Storing an integer (4 or 8 bytes) is more efficient than storing a formatted date string like "2024-07-01T12:00:00Z" (20+ bytes). For large datasets, this difference matters.

4. Universal Compatibility

Every major programming language and database system supports Unix timestamps. They're the common denominator for time representation across different platforms.

How to Convert Timestamp to Epoch

Using JavaScript

JavaScript's Date object works with milliseconds since epoch, not seconds. To convert a date to epoch seconds:

// Current time in epoch seconds
const epochSeconds = Math.floor(Date.now() / 1000);
console.log(epochSeconds); // e.g., 1719792000

// Convert a specific date to epoch
const date = new Date('2024-07-01T00:00:00Z');
const epoch = Math.floor(date.getTime() / 1000);
console.log(epoch); // 1719792000

To convert epoch back to a date:

const epoch = 1719792000;
const date = new Date(epoch * 1000);
console.log(date.toISOString()); // "2024-07-01T00:00:00.000Z"
console.log(date.toLocaleString()); // Local time representation

Using Python

Python's time and datetime modules make epoch conversion straightforward:

import time
from datetime import datetime, timezone

# Current epoch time
epoch = int(time.time())
print(epoch)  # e.g., 1719792000

# Convert epoch to datetime
dt = datetime.fromtimestamp(epoch, tz=timezone.utc)
print(dt)  # 2024-07-01 00:00:00+00:00

# Convert datetime to epoch
dt = datetime(2024, 7, 1, tzinfo=timezone.utc)
epoch = int(dt.timestamp())
print(epoch)  # 1719792000

Using Command Line (Unix/Linux/macOS)

The date command is your friend for quick conversions:

# Current epoch time
date +%s
# Output: 1719792000

# Convert epoch to readable date
date -d @1719792000
# Output: Mon Jul  1 00:00:00 UTC 2024

# Convert specific date to epoch
date -d "2024-07-01 00:00:00" +%s
# Output: 1719792000

On macOS, the syntax is slightly different:

# Convert epoch to date (macOS)
date -r 1719792000

# Convert date to epoch (macOS)
date -j -f "%Y-%m-%d %H:%M:%S" "2024-07-01 00:00:00" +%s

Using Online Tools

For quick conversions without writing code, online epoch converters are invaluable. The FastUnix Timestamp Converter provides instant conversion between epoch timestamps and human-readable dates, supporting both seconds and milliseconds formats.

Common Pitfalls and How to Avoid Them

1. Seconds vs. Milliseconds

The most common mistake is confusing seconds and milliseconds. JavaScript uses milliseconds, while Unix systems and most APIs use seconds.

// ❌ Wrong: treating milliseconds as seconds
const wrongDate = new Date(1719792000); // Year 1970!

// ✅ Correct: multiply by 1000
const correctDate = new Date(1719792000 * 1000); // July 1, 2024

Rule of thumb: If your timestamp has 10 digits, it's seconds. If it has 13 digits, it's milliseconds.

2. Time Zone Confusion

Epoch timestamps are always in UTC. When you convert to local time, the result depends on your system's time zone setting.

from datetime import datetime, timezone

epoch = 1719792000

# UTC time
utc_time = datetime.fromtimestamp(epoch, tz=timezone.utc)
print(utc_time)  # 2024-07-01 00:00:00+00:00

# Local time (depends on system timezone)
local_time = datetime.fromtimestamp(epoch)
print(local_time)  # 2024-07-01 08:00:00 (if in UTC+8)

Always specify the time zone explicitly to avoid bugs.

3. The Year 2038 Problem

32-bit signed integers can only hold values up to 2,147,483,647. On January 19, 2038, at 03:14:07 UTC, Unix timestamps will overflow and wrap around to negative numbers.

Most modern systems use 64-bit integers, which won't overflow for 290 billion years. But legacy systems and embedded devices may still be affected.

Practical Use Cases

API Development

REST APIs commonly use epoch timestamps for date fields:

{
  "id": 12345,
  "created_at": 1719792000,
  "updated_at": 1719878400,
  "expires_at": 1722470400
}

When consuming such APIs, convert the epoch to a user-friendly format:

function formatDate(epoch) {
  const date = new Date(epoch * 1000);
  return date.toLocaleDateString('en-US', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  });
}

console.log(formatDate(1719792000)); // "July 1, 2024"

Database Queries

When working with databases, you often need to filter records by date ranges:

-- PostgreSQL: Find records from the last 7 days
SELECT * FROM events
WHERE created_at > EXTRACT(EPOCH FROM NOW() - INTERVAL '7 days');

-- MySQL: Convert epoch to date for display
SELECT FROM_UNIXTIME(created_at) AS readable_date
FROM events;

Log Analysis

Server logs often contain epoch timestamps. Converting them helps with debugging:

# Convert all timestamps in a log file
awk '{print strftime("%Y-%m-%d %H:%M:%S", $1), $2}' access.log

Epoch Time in Different Systems

JavaScript

  • Date.now() returns milliseconds since epoch
  • new Date(timestamp) expects milliseconds
  • Always multiply/divide by 1000 when working with seconds

Python

  • time.time() returns seconds as a float
  • datetime.timestamp() returns seconds as a float
  • Use int() to get integer seconds

Java

  • System.currentTimeMillis() returns milliseconds
  • Instant.getEpochSecond() returns seconds
  • Java 8+ java.time API provides clean epoch conversion

PHP

  • time() returns current epoch seconds
  • strtotime() converts date strings to epoch
  • date() formats epoch to readable strings

Ruby

  • Time.now.to_i returns epoch seconds
  • Time.at(epoch) converts epoch to Time object

Quick Reference

Operation JavaScript Python Unix Command
Current epoch Math.floor(Date.now()/1000) int(time.time()) date +%s
Epoch to date new Date(epoch*1000) datetime.fromtimestamp(epoch) date -d @epoch
Date to epoch Math.floor(date.getTime()/1000) int(dt.timestamp()) date -d "..." +%s

Conclusion

Epoch timestamps are a fundamental concept in software development. They provide a simple, universal way to represent moments in time without the complexity of time zones and calendar systems.

Key takeaways:

  • Epoch time counts seconds since January 1, 1970, 00:00:00 UTC
  • Always clarify whether you're working with seconds or milliseconds
  • Epoch timestamps are time zone agnostic—conversion to local time happens at display time
  • Use online converters like FastUnix for quick lookups

Whether you're debugging a timestamp bug, building a time-based feature, or analyzing log files, understanding epoch time conversion is an essential skill for every developer.

For more developer tools and timestamp utilities, explore our JSON Formatter and URL Encoder/Decoder.