X Xerobit

Date to Unix Timestamp — Convert Any Date to Unix Time

Converting a date to a Unix timestamp gives you the seconds since January 1, 1970 UTC. Here's how to convert dates to timestamps in JavaScript, Python, SQL, and why timezone...

Mian Ali Khalid · · 5 min read
Use the tool
Timestamp Converter
Convert Unix timestamps, epoch seconds/milliseconds, and ISO 8601 dates.
Open Timestamp Converter →

Converting a date to a Unix timestamp gives you the number of seconds elapsed since January 1, 1970 00:00:00 UTC. The result is timezone-independent and universally understood by programming languages, databases, and APIs.

Use the Timestamp Converter to convert any date and time to a Unix timestamp instantly.

The critical timezone question

Before converting, you must decide: what timezone is the input date in?

2024-05-11 00:00:00 is ambiguous — it could be:

  • 2024-05-11 00:00:00 UTC → timestamp 1715385600
  • 2024-05-11 00:00:00 EDT (UTC-4) → timestamp 1715400000
  • 2024-05-11 00:00:00 JST (UTC+9) → timestamp 1715352600

These are three different moments in time. Always specify the timezone when converting a date string to a timestamp.

Converting in code

JavaScript

// From UTC date string:
const date = new Date('2024-05-11T00:00:00Z');
const timestamp = Math.floor(date.getTime() / 1000);
console.log(timestamp); // 1715385600

// From local time (uses local timezone):
const localDate = new Date('2024-05-11');
const localTimestamp = Math.floor(localDate.getTime() / 1000);
// Result depends on the machine's timezone — avoid this

// From a specific timezone (using Intl API):
// Best approach: construct in UTC explicitly
const utcDate = Date.UTC(2024, 4, 11, 0, 0, 0); // Month is 0-indexed: 4 = May
console.log(Math.floor(utcDate / 1000)); // 1715385600

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

Python

from datetime import datetime, timezone, timedelta
import calendar

# From UTC datetime:
dt_utc = datetime(2024, 5, 11, 0, 0, 0, tzinfo=timezone.utc)
timestamp = int(dt_utc.timestamp())
print(timestamp)  # 1715385600

# From ISO 8601 string with timezone:
dt = datetime.fromisoformat('2024-05-11T00:00:00+00:00')
timestamp = int(dt.timestamp())
print(timestamp)  # 1715385600

# From a timezone-aware datetime (non-UTC):
eastern = timezone(timedelta(hours=-4))
dt_eastern = datetime(2024, 5, 11, 0, 0, 0, tzinfo=eastern)
timestamp = int(dt_eastern.timestamp())
print(timestamp)  # 1715400000 (4 hours later than UTC)

# WRONG: timezone-naive datetime (assumes local time):
dt_naive = datetime(2024, 5, 11, 0, 0, 0)  # No timezone!
timestamp_wrong = int(dt_naive.timestamp())  # Depends on system timezone

SQL

-- PostgreSQL: date string to Unix timestamp
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2024-05-11 00:00:00 UTC')::INT;
-- 1715385600

SELECT EXTRACT(EPOCH FROM '2024-05-11T00:00:00+00:00'::TIMESTAMPTZ)::INT;
-- 1715385600

-- MySQL:
SELECT UNIX_TIMESTAMP('2024-05-11 00:00:00');
-- Note: MySQL UNIX_TIMESTAMP converts from server's local timezone
-- To specify UTC:
SELECT UNIX_TIMESTAMP(CONVERT_TZ('2024-05-11 00:00:00', '+00:00', @@session.time_zone));

-- SQLite:
SELECT strftime('%s', '2024-05-11T00:00:00Z');
-- 1715385600

Go

import "time"

// From UTC:
t := time.Date(2024, time.May, 11, 0, 0, 0, 0, time.UTC)
timestamp := t.Unix()
fmt.Println(timestamp) // 1715385600

// From time string:
t2, _ := time.Parse(time.RFC3339, "2024-05-11T00:00:00Z")
fmt.Println(t2.Unix()) // 1715385600

Common conversions

2024-01-01 00:00:00 UTC → 1704067200
2024-01-01 00:00:00 EST → 1704085200
2024-06-15 12:00:00 UTC → 1718452800
2025-01-01 00:00:00 UTC → 1735689600

Midnight in a specific timezone

A frequent use case: “give me a timestamp for the start of today in UTC” or “the start of the business day in New York.”

from datetime import datetime, timezone, timedelta
from zoneinfo import ZoneInfo  # Python 3.9+

# Start of today in UTC:
now = datetime.now(timezone.utc)
start_of_day_utc = now.replace(hour=0, minute=0, second=0, microsecond=0)
print(int(start_of_day_utc.timestamp()))

# Start of today in New York (for a New York business):
tz_ny = ZoneInfo('America/New_York')
now_ny = datetime.now(tz_ny)
start_of_day_ny = now_ny.replace(hour=0, minute=0, second=0, microsecond=0)
print(int(start_of_day_ny.timestamp()))
# Different result if NY is behind UTC (e.g., UTC-4 in EDT)

Converting with milliseconds

Some systems use milliseconds:

// JavaScript uses milliseconds:
Date.now()                            // milliseconds since epoch
Math.floor(Date.now() / 1000)        // seconds

const date = new Date('2024-05-11T00:00:00Z');
date.getTime()                        // milliseconds: 1715385600000
Math.floor(date.getTime() / 1000)    // seconds: 1715385600
# Python: multiply seconds by 1000 for milliseconds
import time
timestamp_ms = int(time.time() * 1000)  # Current milliseconds timestamp

Verifying your conversion

The Timestamp Converter converts in both directions — paste the timestamp to see if it resolves to the date you intended, or enter a date to get the timestamp. Always verify the result, especially when timezone conversions are involved.


Related posts

Related tool

Timestamp Converter

Convert Unix timestamps, epoch seconds/milliseconds, and ISO 8601 dates.

Written by Mian Ali Khalid. Part of the Dev Productivity pillar.