Skip to main content
12 min read

Unix Timestamp Explained: A Developer's Complete Guide

Unix timestamps are everywhere in software development. This guide covers everything you need to know about working with epoch time, from basic concepts to advanced conversions.

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, 00:00:00 UTC. This specific moment is called the "Unix Epoch."

For example, the timestamp 1709136000 represents February 28, 2024, 12:00:00 PM UTC.

Current Unix Timestamp

Use our timestamp converter to get the current Unix timestamp and convert between formats.

Why Use Unix Timestamps?

Unix timestamps are popular in software development for several reasons:

  • Timezone-independent: Timestamps represent a single moment in time, regardless of timezone
  • Easy to compare: Simple integer comparison tells you which time is earlier or later
  • Compact storage: A 32-bit or 64-bit integer is smaller than a formatted date string
  • Language-agnostic: Every programming language can work with integers
  • No ambiguity: Unlike "02/03/2024" which could be Feb 3 or Mar 2, timestamps are unambiguous

Working with Timestamps in Different Languages

JavaScript

// Get current timestamp (milliseconds)
const now = Date.now();

// Get current timestamp (seconds)
const nowSeconds = Math.floor(Date.now() / 1000);

// Convert timestamp to Date
const date = new Date(timestamp * 1000);

// Convert Date to timestamp
const timestamp = Math.floor(date.getTime() / 1000);

// Format for display
console.log(date.toISOString());
// "2026-02-28T12:00:00.000Z"

Note: JavaScript's Date.now() returns milliseconds, not seconds. Divide by 1000 for Unix timestamps.

Python

import time
from datetime import datetime

# Get current timestamp
timestamp = int(time.time())

# Convert timestamp to datetime
dt = datetime.fromtimestamp(timestamp)

# Convert datetime to timestamp
timestamp = int(dt.timestamp())

# Format for display
print(dt.isoformat())
# "2026-02-28T12:00:00"

PHP

<?php
// Get current timestamp
$timestamp = time();

// Convert timestamp to date
$date = date('Y-m-d H:i:s', $timestamp);

// Convert date to timestamp
$timestamp = strtotime('2026-02-28 12:00:00');

// Format for display
echo date('c', $timestamp);
// "2026-02-28T12:00:00+00:00"
?>

Go

package main

import (
    "fmt"
    "time"
)

func main() {
    // Get current timestamp
    timestamp := time.Now().Unix()
    
    // Convert timestamp to time.Time
    t := time.Unix(timestamp, 0)
    
    // Format for display
    fmt.Println(t.Format(time.RFC3339))
    // "2026-02-28T12:00:00Z"
}

Milliseconds vs Seconds

One common source of confusion is the difference between timestamps in seconds and milliseconds:

  • Unix timestamp (seconds): 1709136000 (10 digits)
  • JavaScript timestamp (milliseconds): 1709136000000 (13 digits)

Always check the length of a timestamp to know which format you're dealing with. Our converter tool automatically detects the format.

The Year 2038 Problem

Unix timestamps stored in a signed 32-bit integer will overflow on January 19, 2038, at 03:14:07 UTC. After this point, the timestamp will become negative, causing dates to jump back to 1901.

The solution is to use 64-bit integers, which most modern systems already do. A 64-bit timestamp won't overflow for about 292 billion years.

⚠️ Warning: If you're working with legacy 32-bit systems or databases, check that your timestamp fields can handle dates beyond 2038.

ISO 8601 vs Unix Timestamps

Another common time format is ISO 8601, which looks like 2026-02-28T12:00:00Z. Here's when to use each:

Use CaseRecommended Format
Database storageUnix timestamp or ISO 8601
API responses (for machines)Unix timestamp
API responses (for humans)ISO 8601
Log filesISO 8601 (more readable)
Sorting/comparing datesUnix timestamp

Common Timestamp Operations

Add Days to a Timestamp

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

Get Start of Day

// Get start of current day (midnight UTC)
const startOfDay = Math.floor(timestamp / 86400) * 86400;

Calculate Time Difference

// Difference in seconds
const diff = timestamp2 - timestamp1;

// Convert to human-readable
const days = Math.floor(diff / 86400);
const hours = Math.floor((diff % 86400) / 3600);
const minutes = Math.floor((diff % 3600) / 60);

For complex duration calculations, use our Duration Calculator.

Common Pitfalls

1. Timezone Confusion

Unix timestamps are always in UTC. When displaying to users, convert to their local timezone.

2. Milliseconds vs Seconds

Always check whether your timestamp is in seconds or milliseconds. A timestamp of 1709136000000 (13 digits) is milliseconds.

3. Negative Timestamps

Dates before January 1, 1970, have negative timestamps. Make sure your system handles these correctly.

4. Floating Point Precision

When dealing with millisecond timestamps in JavaScript, be aware of floating-point precision issues with very large numbers.

Online Tools

Instead of writing conversion code, use online tools for quick conversions:

Convert Timestamps Instantly

Use our free timestamp converter for quick Unix/ISO conversions.

Open Code Tools