ToolPopToolPop
Back to BlogTutorials

Timestamp Converter: Complete Guide to Unix Time and Date Handling

Unix timestamps are universal in programming. Learn how to convert, manipulate, and format timestamps correctly in any language.

ToolPop TeamMarch 14, 202512 min read

What is a Unix Timestamp?

A Unix timestamp (or Epoch time) is the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC. It's a universal way to represent a point in time, independent of time zones.

Examples

1711929600 = April 1, 2024, 00:00:00 UTC
0          = January 1, 1970, 00:00:00 UTC
-86400     = December 31, 1969, 00:00:00 UTC

Why Unix Time?

  • Universal: Same value everywhere in the world
  • Simple: Just a number, easy to store and compare
  • Compact: Takes less storage than date strings
  • Sortable: Natural numeric sorting

Timestamp Formats

Seconds (Standard Unix)

1711929600

Most databases and APIs use seconds.

Milliseconds

1711929600000

JavaScript's Date.now() returns milliseconds.

Microseconds

1711929600000000

Used in some databases and high-precision systems.

Nanoseconds

1711929600000000000

Used in some programming languages (Go) and databases.

Getting Current Timestamp

JavaScript

// Milliseconds
Date.now()                    // 1711929600000

// Seconds
Math.floor(Date.now() / 1000) // 1711929600

// From Date object
new Date().getTime()          // 1711929600000

Python

import time
from datetime import datetime

# Seconds (float)
time.time()                   # 1711929600.123456

# Seconds (integer)
int(time.time())              # 1711929600

# From datetime
datetime.now().timestamp()    # 1711929600.123456

PHP

// Current timestamp
time();                       // 1711929600

// Milliseconds
round(microtime(true) * 1000);

Java

// Milliseconds
System.currentTimeMillis();   // 1711929600000L

// Seconds
Instant.now().getEpochSecond(); // 1711929600L

Go

import "time"

// Seconds
time.Now().Unix()      // 1711929600

// Nanoseconds
time.Now().UnixNano()  // 1711929600000000000

SQL

-- MySQL
SELECT UNIX_TIMESTAMP();

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

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

Converting Timestamps

Timestamp to Date

JavaScript:

// From seconds
new Date(1711929600 * 1000)

// From milliseconds
new Date(1711929600000)

// Formatted
new Date(1711929600000).toISOString()
// "2024-04-01T00:00:00.000Z"

new Date(1711929600000).toLocaleString()
// "4/1/2024, 12:00:00 AM" (locale-dependent)

Python:

from datetime import datetime

# From timestamp
dt = datetime.fromtimestamp(1711929600)
# datetime(2024, 4, 1, 0, 0)

# UTC
dt_utc = datetime.utcfromtimestamp(1711929600)

# Formatted
dt.strftime('%Y-%m-%d %H:%M:%S')
# "2024-04-01 00:00:00"

PHP:

// From timestamp
date('Y-m-d H:i:s', 1711929600);
// "2024-04-01 00:00:00"

// DateTime object
$dt = new DateTime("@1711929600");

Date to Timestamp

JavaScript:

// From date string
new Date('2024-04-01').getTime() / 1000
// 1711929600

// From components
new Date(2024, 3, 1).getTime() / 1000  // Note: month is 0-indexed

Python:

from datetime import datetime

# From string
dt = datetime.strptime('2024-04-01', '%Y-%m-%d')
timestamp = dt.timestamp()

# From components
dt = datetime(2024, 4, 1)
timestamp = dt.timestamp()

Time Zones

The Importance of UTC

Always store timestamps in UTC:

// ❌ Local time (ambiguous)
new Date('2024-04-01 12:00:00')

// ✅ UTC (unambiguous)
new Date('2024-04-01T12:00:00Z')

Converting Time Zones

JavaScript:

const date = new Date(1711929600000);

// Different time zones
date.toLocaleString('en-US', { timeZone: 'America/New_York' })
// "3/31/2024, 8:00:00 PM"

date.toLocaleString('en-US', { timeZone: 'Asia/Tokyo' })
// "4/1/2024, 9:00:00 AM"

Python:

from datetime import datetime
import pytz

utc_dt = datetime.utcfromtimestamp(1711929600)
utc_dt = utc_dt.replace(tzinfo=pytz.UTC)

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

Date Formatting

ISO 8601 (Recommended)

2024-04-01T12:30:45Z
2024-04-01T12:30:45+05:30
2024-04-01T12:30:45.123Z

Common Formats

FormatExample
ISO 86012024-04-01T12:30:45Z
RFC 2822Mon, 01 Apr 2024 12:30:45 +0000
Unix1711973445
US04/01/2024
EU01/04/2024

Format Specifiers

Python strftime / strptime:

CodeMeaningExample
%Y4-digit year2024
%mMonth (01-12)04
%dDay (01-31)01
%HHour (00-23)12
%MMinute (00-59)30
%SSecond (00-59)45
%zUTC offset+0000
JavaScript Intl.DateTimeFormat:
new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: '2-digit',
  minute: '2-digit',
  timeZone: 'UTC'
}).format(new Date(1711929600000))
// "April 1, 2024, 12:00 AM"

Common Operations

Add/Subtract Time

JavaScript:

const date = new Date(1711929600000);

// Add 1 day
date.setDate(date.getDate() + 1);

// Add 2 hours
date.setHours(date.getHours() + 2);

// With timestamps
const timestamp = 1711929600;
const plusOneDay = timestamp + (24 * 60 * 60);
const plusTwoHours = timestamp + (2 * 60 * 60);

Python:

from datetime import datetime, timedelta

dt = datetime.fromtimestamp(1711929600)

# Add 1 day
dt + timedelta(days=1)

# Add 2 hours
dt + timedelta(hours=2)

Difference Between Dates

const date1 = new Date('2024-04-01');
const date2 = new Date('2024-04-15');

// Difference in milliseconds
const diffMs = date2 - date1;

// Difference in days
const diffDays = diffMs / (1000 * 60 * 60 * 24);
// 14

Start/End of Day

const date = new Date(1711929600000);

// Start of day (UTC)
const startOfDay = new Date(Date.UTC(
  date.getUTCFullYear(),
  date.getUTCMonth(),
  date.getUTCDate()
));

// End of day (UTC)
const endOfDay = new Date(Date.UTC(
  date.getUTCFullYear(),
  date.getUTCMonth(),
  date.getUTCDate(),
  23, 59, 59, 999
));

Database Storage

Best Practices

ApproachProsCons
Unix timestamp (INT)Simple, sortableLimited precision
DATETIME/TIMESTAMPHuman-readableTime zone issues
ISO 8601 stringUniversalLarger storage

MySQL

-- Store as TIMESTAMP (auto UTC conversion)
CREATE TABLE events (
  id INT PRIMARY KEY,
  event_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Convert to/from Unix
SELECT UNIX_TIMESTAMP(event_time) FROM events;
SELECT FROM_UNIXTIME(1711929600);

PostgreSQL

-- Use TIMESTAMPTZ for timezone-aware
CREATE TABLE events (
  id SERIAL PRIMARY KEY,
  event_time TIMESTAMPTZ DEFAULT NOW()
);

-- Convert to/from epoch
SELECT EXTRACT(EPOCH FROM event_time) FROM events;
SELECT TO_TIMESTAMP(1711929600);

Common Pitfalls

1. JavaScript Month Indexing

// ❌ Wrong: This is May, not April!
new Date(2024, 4, 1)

// ✅ Correct: April (0-indexed)
new Date(2024, 3, 1)

2. Time Zone Confusion

// ❌ Ambiguous (local time)
new Date('2024-04-01 12:00:00')

// ✅ Explicit UTC
new Date('2024-04-01T12:00:00Z')

// ✅ Explicit timezone
new Date('2024-04-01T12:00:00+05:30')

3. Seconds vs Milliseconds

// ❌ Treating seconds as milliseconds
new Date(1711929600)  // Wrong! Jan 20, 1970

// ✅ Convert seconds to milliseconds
new Date(1711929600 * 1000)  // Correct! Apr 1, 2024

4. Y2K38 Problem

32-bit Unix timestamps overflow on January 19, 2038:

Maximum 32-bit: 2147483647 = 2038-01-19 03:14:07 UTC

Use 64-bit timestamps for future dates.

Conclusion

Unix timestamps provide a simple, universal way to represent time. Remember to always store in UTC, be explicit about time zones, and watch out for seconds vs milliseconds confusion.

Use our free Timestamp Converter tool to quickly convert between Unix timestamps and human-readable dates during development.

Tags
timestamp converterunix timestampepoch timedate convertertime converterunix timeepoch converter
Share this article

Try Our Free Tools

Put these tips into practice with our free online tools. No signup required.

Explore Tools