Unix Timestamp Guide: Everything Developers Need to Know

Published February 4, 2026 • 12 min read

Unix timestamps are the universal language of time in computing. If you've ever worked with APIs, databases, or date/time programming, you've encountered them. This comprehensive guide explains what Unix timestamps are, how to use them, and how to avoid common pitfalls.

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 (the "Unix Epoch").

Current Unix timestamp: 1738713600
↓
Represents: February 4, 2026, 23:20:00 UTC

Key characteristics:

Why Unix Timestamps?

✅ Advantages

❌ Disadvantages

Unix Timestamp Formats

Seconds (Standard)

1738713600  →  Seconds since epoch
Most common format, used by most systems

Milliseconds (JavaScript)

1738713600000  →  Milliseconds since epoch
JavaScript Date.now() returns this format

Microseconds

1738713600000000  →  Microseconds since epoch
Used in high-precision applications

Nanoseconds

1738713600000000000  →  Nanoseconds since epoch
Go's time.Now().UnixNano()

Getting Current Unix Timestamp

JavaScript

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

// Milliseconds (JavaScript default)
const timestampMs = Date.now();

// Alternative
const timestamp = Math.floor(new Date().getTime() / 1000);

Python

import time

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

# Using datetime
from datetime import datetime
timestamp = int(datetime.now().timestamp())

PHP

// Seconds
$timestamp = time();

// Milliseconds
$timestamp = round(microtime(true) * 1000);

Java

// Seconds
long timestamp = System.currentTimeMillis() / 1000L;

// Milliseconds
long timestampMs = System.currentTimeMillis();

// Java 8+
long timestamp = Instant.now().getEpochSecond();

Go

import "time"

// Seconds
timestamp := time.Now().Unix()

// Milliseconds
timestampMs := time.Now().UnixMilli()

// Nanoseconds
timestampNs := time.Now().UnixNano()

SQL

-- MySQL
SELECT UNIX_TIMESTAMP();

-- PostgreSQL
SELECT EXTRACT(EPOCH FROM NOW());

-- SQLite
SELECT strftime('%s', 'now');

Shell / Bash

# Linux/macOS
date +%s

# Specific date
date -d "2026-02-04" +%s

Converting Unix Timestamp to Human-Readable Date

JavaScript

const timestamp = 1738713600;

// Create Date object
const date = new Date(timestamp * 1000);

// Format options
date.toISOString();           // "2026-02-04T23:20:00.000Z"
date.toLocaleDateString();    // "2/4/2026" (locale-specific)
date.toLocaleString();        // "2/4/2026, 11:20:00 PM"

// Custom format
const options = { 
  year: 'numeric', 
  month: 'long', 
  day: 'numeric',
  hour: '2-digit',
  minute: '2-digit'
};
date.toLocaleDateString('en-US', options);  // "February 4, 2026 at 11:20 PM"

Python

from datetime import datetime

timestamp = 1738713600

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

# Format
dt.strftime('%Y-%m-%d %H:%M:%S')  # "2026-02-04 23:20:00"
dt.strftime('%B %d, %Y')          # "February 04, 2026"

# ISO format
dt.isoformat()  # "2026-02-04T23:20:00"

PHP

$timestamp = 1738713600;

// Format
date('Y-m-d H:i:s', $timestamp);  // "2026-02-04 23:20:00"
date('F j, Y', $timestamp);       // "February 4, 2026"

// DateTime object
$dt = new DateTime("@$timestamp");
echo $dt->format('Y-m-d H:i:s');

Go

timestamp := int64(1738713600)

// Convert to Time
t := time.Unix(timestamp, 0)

// Format
t.Format("2006-01-02 15:04:05")  // "2026-02-04 23:20:00"
t.Format(time.RFC3339)           // "2026-02-04T23:20:00Z"

Converting Date to Unix Timestamp

JavaScript

// From date string
const timestamp = Math.floor(new Date('2026-02-04').getTime() / 1000);

// From components
const timestamp = Math.floor(new Date(2026, 1, 4).getTime() / 1000);  // Month is 0-indexed!

Python

from datetime import datetime

# From string
dt = datetime.strptime('2026-02-04', '%Y-%m-%d')
timestamp = int(dt.timestamp())

# From components
dt = datetime(2026, 2, 4)
timestamp = int(dt.timestamp())

PHP

// From string
$timestamp = strtotime('2026-02-04');

// From components
$timestamp = mktime(0, 0, 0, 2, 4, 2026);

Working with Timezones

Important: Unix timestamps are always UTC. Timezone conversion happens during display, not storage.

JavaScript with Timezones

const timestamp = 1738713600;
const date = new Date(timestamp * 1000);

// Display in specific timezone
const options = { 
  timeZone: 'America/New_York',
  year: 'numeric',
  month: 'long', 
  day: 'numeric',
  hour: '2-digit',
  minute: '2-digit',
  timeZoneName: 'short'
};

date.toLocaleString('en-US', options);
// "February 4, 2026 at 6:20 PM EST"

Python with Timezones

from datetime import datetime
import pytz

timestamp = 1738713600

# UTC
utc_dt = datetime.fromtimestamp(timestamp, tz=pytz.UTC)

# Convert to specific timezone
ny_tz = pytz.timezone('America/New_York')
ny_dt = utc_dt.astimezone(ny_tz)

print(ny_dt.strftime('%Y-%m-%d %H:%M:%S %Z'))
# "2026-02-04 18:20:00 EST"

Common Calculations

Time Difference

const now = Math.floor(Date.now() / 1000);
const past = 1738713600;

const diffSeconds = now - past;
const diffMinutes = diffSeconds / 60;
const diffHours = diffMinutes / 60;
const diffDays = diffHours / 24;

Add Time

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

// Add 1 hour
const oneHourLater = now + (60 * 60);

// Add 1 day
const oneDayLater = now + (24 * 60 * 60);

// Add 1 week
const oneWeekLater = now + (7 * 24 * 60 * 60);

Relative Time ("3 hours ago")

function timeAgo(timestamp) {
  const now = Math.floor(Date.now() / 1000);
  const diff = now - timestamp;
  
  if (diff < 60) return `${diff} seconds ago`;
  if (diff < 3600) return `${Math.floor(diff / 60)} minutes ago`;
  if (diff < 86400) return `${Math.floor(diff / 3600)} hours ago`;
  return `${Math.floor(diff / 86400)} days ago`;
}

timeAgo(1738713600);  // "3 hours ago"

The Year 2038 Problem

32-bit signed integers can only represent timestamps up to January 19, 2038, 03:14:07 UTC.

Max 32-bit timestamp: 2,147,483,647
Date: 2038-01-19 03:14:07 UTC

Next second: Integer overflow → -2,147,483,648
Date wraps to: 1901-12-13

Solution: Use 64-bit integers (good until year 292 billion).

Most modern systems have already transitioned to 64-bit timestamps, but legacy code and embedded systems may still be affected.

Best Practices

1. Always Store in UTC

✅ Store: Unix timestamp (UTC)
✅ Display: Convert to user's timezone

❌ Store: Local time
❌ Store: Date strings without timezone

2. Use Seconds for APIs

Unless you need higher precision, use seconds for interoperability.

3. Be Explicit About Format

// Document what format you're using
{
  "created_at": 1738713600,       // Unix timestamp (seconds)
  "created_at_ms": 1738713600000  // Unix timestamp (milliseconds)
}

4. Never Do Timezone Math Manually

Use libraries! Don't try to add/subtract hours for timezone conversion.

❌ Wrong:
const nycTime = utcTimestamp + (5 * 60 * 60);  // Breaks during DST!

✅ Correct:
Use date libraries with timezone support (date-fns-tz, Luxon, moment-timezone)

5. Validate Timestamps

function isValidTimestamp(timestamp) {
  // Reasonable range: 2000-01-01 to 2100-01-01
  const min = 946684800;    // 2000-01-01
  const max = 4102444800;   // 2100-01-01
  return timestamp >= min && timestamp <= max;
}

Common Mistakes

1. Confusing Seconds and Milliseconds

// JavaScript returns milliseconds!
const wrong = Date.now();  // 1738713600000
const right = Math.floor(Date.now() / 1000);  // 1738713600

2. Ignoring Timezones

❌ "February 4 at 6pm" - What timezone?
✅ Store: 1738713600 (UTC)
✅ Display: "February 4 at 6pm EST" (with user's timezone)

3. String Comparison Instead of Numeric

❌ if (timestamp1 > timestamp2)  // Strings compare wrong
✅ if (Number(timestamp1) > Number(timestamp2))

Testing & Debugging

Use our Unix Timestamp Converter to:

Real-World Use Cases

Session Expiration

const expiresAt = Math.floor(Date.now() / 1000) + (24 * 60 * 60);  // 24 hours

function isExpired(expiresAt) {
  return Math.floor(Date.now() / 1000) > expiresAt;
}

Rate Limiting

const requests = [
  { timestamp: 1738713600 },
  { timestamp: 1738713650 },
  { timestamp: 1738713700 }
];

// Count requests in last minute
const now = Math.floor(Date.now() / 1000);
const recentRequests = requests.filter(r => now - r.timestamp < 60);

if (recentRequests.length >= 10) {
  throw new Error('Rate limit exceeded');
}

Database Queries

// Get records from last 7 days
const sevenDaysAgo = Math.floor(Date.now() / 1000) - (7 * 24 * 60 * 60);

SELECT * FROM posts 
WHERE created_at > ${sevenDaysAgo}
ORDER BY created_at DESC;

Conclusion

Unix timestamps are the foundation of time handling in modern computing. While not human-readable, they're simple, universal, and eliminate timezone ambiguity when used correctly.

Key takeaways:

Convert Timestamps Instantly

Use our free Unix Timestamp Converter to convert between Unix timestamps and human-readable dates in any timezone.

Back to Blog