DEV Community

ze he
ze he

Posted on • Originally published at aiforeverthing.com

Unix Timestamp Converter: Complete Guide to Converting Epoch Time

Unix Timestamp Converter: Complete Guide to Converting Epoch Time

Unix timestamps (also called epoch time or POSIX time) are a fundamental concept in programming. They represent time as the number of seconds since January 1, 1970, 00:00:00 UTC.

What is a Unix Timestamp?

A Unix timestamp is a single integer that represents a specific moment in time:

1711084800  →  March 22, 2024 00:00:00 UTC
Enter fullscreen mode Exit fullscreen mode

Why use timestamps?

  • Universal: Works across all timezones and programming languages
  • Compact: Single number instead of complex date objects
  • Sortable: Chronological ordering is just numeric sorting
  • Math-friendly: Easy to calculate durations (subtraction)

Seconds vs Milliseconds

Different systems use different precision:

System Format Example
Unix/Linux Seconds 1711084800
JavaScript Milliseconds 1711084800000
Python Seconds (float) 1711084800.123
Java Milliseconds 1711084800000L

How to tell them apart:

  • Seconds: 10 digits (around 1-2 billion)
  • Milliseconds: 13 digits (around 1-2 trillion)

Converting Timestamp to Date

JavaScript

const timestamp = 1711084800;
const date = new Date(timestamp * 1000); // Convert to milliseconds
console.log(date.toISOString()); // "2024-03-22T00:00:00.000Z"
console.log(date.toUTCString());  // "Fri, 22 Mar 2024 00:00:00 GMT"
Enter fullscreen mode Exit fullscreen mode

Python

import datetime

timestamp = 1711084800
dt = datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
print(dt.isoformat())  # "2024-03-22T00:00:00+00:00"
Enter fullscreen mode Exit fullscreen mode

PHP

$timestamp = 1711084800;
echo date('Y-m-d H:i:s', $timestamp); // "2024-03-22 00:00:00"
Enter fullscreen mode Exit fullscreen mode

Converting Date to Timestamp

JavaScript

const date = new Date('2024-03-22 12:00:00');
const timestamp = Math.floor(date.getTime() / 1000);
console.log(timestamp); // 1711108800
Enter fullscreen mode Exit fullscreen mode

Python

import datetime

dt = datetime.datetime(2024, 3, 22, 12, 0, 0, tzinfo=datetime.timezone.utc)
timestamp = int(dt.timestamp())
print(timestamp)  # 1711108800
Enter fullscreen mode Exit fullscreen mode

Common Use Cases

1. API Rate Limiting

const rateLimitReset = 1711090000; // Server response
const now = Math.floor(Date.now() / 1000);
const secondsUntilReset = rateLimitReset - now;
console.log(`Wait ${secondsUntilReset} seconds`);
Enter fullscreen mode Exit fullscreen mode

2. Session Expiry

const sessionExpiry = Math.floor(Date.now() / 1000) + 3600; // Expires in 1 hour
localStorage.setItem('expiry', sessionExpiry);

// Later...
const expiry = parseInt(localStorage.getItem('expiry'));
if (Math.floor(Date.now() / 1000) > expiry) {
  console.log('Session expired');
}
Enter fullscreen mode Exit fullscreen mode

3. Database Timestamps

-- SQLite example
CREATE TABLE events (
  id INTEGER PRIMARY KEY,
  created_at INTEGER DEFAULT (strftime('%s', 'now'))
);

-- Query events from last 24 hours
SELECT * FROM events
WHERE created_at > (strftime('%s', 'now') - 86400);
Enter fullscreen mode Exit fullscreen mode

Important Gotchas

Year 2038 Problem

32-bit signed integers can only store timestamps up to:

2,147,483,647 seconds = January 19, 2038, 03:14:07 UTC
Enter fullscreen mode Exit fullscreen mode

After this, 32-bit systems will overflow (similar to Y2K). Solution: Use 64-bit integers or switch to date objects.

Timezone Confusion

// ❌ WRONG - Creates local time
const date = new Date('2024-03-22 12:00:00');

// ✅ CORRECT - Explicit UTC
const date = new Date('2024-03-22T12:00:00Z');
Enter fullscreen mode Exit fullscreen mode

Always be explicit about timezones when parsing dates!

Milliseconds Auto-Detection

function smartTimestampConvert(timestamp) {
  // If > 10 billion, assume milliseconds
  if (timestamp > 10000000000) {
    timestamp = Math.floor(timestamp / 1000);
  }
  return new Date(timestamp * 1000);
}
Enter fullscreen mode Exit fullscreen mode

Try It Yourself

Use our free Unix Timestamp Converter to:

  • Convert timestamps to human-readable dates
  • Convert dates to Unix timestamps
  • Auto-detect seconds vs milliseconds
  • Support UTC and local timezones
  • Copy formatted outputs instantly

Quick Reference

// Get current timestamp
Math.floor(Date.now() / 1000)

// Convert timestamp → date
new Date(timestamp * 1000)

// Convert date → timestamp
Math.floor(date.getTime() / 1000)

// Add 1 day to timestamp
timestamp + 86400

// Check if timestamp is in the past
timestamp < Math.floor(Date.now() / 1000)
Enter fullscreen mode Exit fullscreen mode

Conclusion

Unix timestamps are the universal language of time in programming. Master these conversions and you'll handle dates with confidence across any system.

Key Takeaways:

  • Timestamps = seconds since Jan 1, 1970 UTC
  • JavaScript uses milliseconds (×1000 larger)
  • Always specify timezones when parsing dates
  • Watch out for the Year 2038 problem on 32-bit systems

Need to convert timestamps right now? Try our free timestamp converter tool — no installation required!

Top comments (0)