Learn how to convert date to epoch (Unix time) in seconds or milliseconds across languages, databases, and tools. Clear steps, examples, best practices, and common pitfalls.
If you’ve ever needed to convert date to epoch, you’re working with Unix time—the number of seconds (or milliseconds) since January 1, 1970 UTC. This guide explains what epoch time is, why it matters, and how to convert dates reliably across languages, databases, command line tools, and spreadsheets—without falling into common timezone or unit traps.
Featured Snippet Answer: To convert date to epoch (Unix time), parse the date in UTC, then compute the seconds since 1970‑01‑01 00:00:00 UTC. In JavaScript: Math.floor(new Date('2024-05-20T15:00:00Z').getTime()/1000). In Python: int(datetime.fromisoformat('2024-05-20T15:00:00+00:00').timestamp()). On Linux: date -d '2024-05-20 15:00:00 UTC' +%s. Always confirm seconds vs milliseconds.
Epoch (Unix) time counts seconds since 1970‑01‑01 UTC. To convert a date to epoch, parse the date in UTC, then output seconds (10 digits) or milliseconds (13 digits). Watch for timezone offsets, daylight saving time, and unit mismatches. This guide shows quick conversions in JavaScript, Python, Bash, SQL, Java, Go, C#, and Excel, plus best practices, edge cases, a comparison table, and FAQs.
“Convert date to epoch” means turning a human-readable date (like 2024‑05‑20 15:00:00) into a Unix timestamp: the count of seconds (or milliseconds) since the Unix epoch: 1970‑01‑01 00:00:00 UTC. Example: 1716217200 (seconds) or 1716217200000 (milliseconds).
Tip: Confirm the expected unit before converting or storing.
JavaScript (Node/Browser)
Math.floor(new Date('2024-05-20T15:00:00Z').getTime() / 1000)
new Date('2024-05-20T15:00:00Z').getTime()
Date.now(); (s): Math.floor(Date.now()/1000)Python (3.11+)
from datetime import datetime, timezone
dt = datetime.fromisoformat('2024-05-20T15:00:00+00:00')
epoch_seconds = int(dt.timestamp())
epoch_millis = int(dt.timestamp() * 1000)
# For naive local times, set tz: dt.replace(tzinfo=timezone.utc)
Bash (GNU date)
date -d '2024-05-20 15:00:00 UTC' +%s
# Now: date +%s
Note: On macOS use gdate from coreutils: brew install coreutils then gdate.
Java
Instant instant = Instant.parse("2024-05-20T15:00:00Z");
long seconds = instant.getEpochSecond();
long millis = instant.toEpochMilli();
Go
t, _ := time.Parse(time.RFC3339, "2024-05-20T15:00:00Z")
secs := t.Unix() // seconds
millis := t.UnixMilli() // milliseconds (Go 1.17+)
C# (.NET)
var dt = DateTime.Parse("2024-05-20T15:00:00Z", null, System.Globalization.DateTimeStyles.AdjustToUniversal);
long seconds = new DateTimeOffset(dt).ToUnixTimeSeconds();
long millis = new DateTimeOffset(dt).ToUnixTimeMilliseconds();
PHP
$dt = new DateTime('2024-05-20T15:00:00Z');
$seconds = $dt->getTimestamp();
$millis = $seconds * 1000; // or use microtime(true)*1000 for now
now >= run_at without time zone issues.now - window_seconds.expires_at as epoch seconds. Check now > expires_at quickly.iat, nbf, and exp as epoch seconds. Compare with now in UTC.bucket = floor(epoch/60)*60 for group-by."2024-05-20T15:00:00Z" to seconds before storing to avoid locale parsing errors.Mixing seconds and milliseconds
Ignoring time zones
Assuming daylight saving time doesn’t matter
Relying on ambiguous date formats
Expecting leap second awareness
Truncating or overflowing
Not documenting time precision
Date.parse, Temporal (when available), or libraries like Luxon/Day.js for strict parsing.datetime objects; avoid naive UTC assumptions.| Platform/Language | Function/Command | Unit | Time Zone Default | Example Input |
|---|---|---|---|---|
| JavaScript | Date.getTime() | ms | Local if parsed without Z; UTC with Z | new Date('2024-05-20T15:00:00Z') |
| Python | datetime.timestamp() | s | As per tzinfo; naive may assume local | fromisoformat('...+00:00') |
| Bash (GNU) | date +%s | s | System TZ unless specified | -d '... UTC' |
| Java | Instant.parse() | s/ms | UTC | Instant.parse('...Z') |
| Go | t.Unix()/UnixMilli() | s/ms | As parsed; RFC3339 Z => UTC | time.Parse(time.RFC3339, '...Z') |
| C# | ToUnixTimeSeconds() |
Math.floor(Date.now()/1000). On Linux: date +%s.new Date(1716217200*1000).toISOString(); Python: datetime.utcfromtimestamp(1716217200).isoformat()+"Z".UNIX_TIMESTAMP() uses the session time zone. Set time_zone='+00:00' or provide UTC timestamps.Converting dates to Unix timestamps is simple once you control units and time zones. Standardize inputs with ISO 8601, parse in UTC, and choose seconds or milliseconds—then stick to it. Whether you script, query, or analyze, mastering how to convert date to epoch prevents subtle bugs and keeps your systems in sync.
Need a fast, reliable converter and formatter? Try ZenixTools’ Epoch & Time Utilities to convert date to epoch, switch time zones, and validate formats—right in your browser.
A practical, expert guide to convert base64 to string across languages, with steps, examples, pitfalls, and best practices.
A practical, expert guide to convert Base64 string to text or files with JavaScript, Python, CLI, and more. Includes steps, examples, mistakes to avoid, best practices, FAQs, and a comparison table.
Ruby
require 'time'
t = Time.parse('2024-05-20T15:00:00Z')
seconds = t.to_i
millis = (t.to_f * 1000).to_i
SQL (MySQL/MariaDB)
SELECT UNIX_TIMESTAMP('2024-05-20 15:00:00') AS seconds_utc; -- Interprets in session TZ unless you specify UTC
SELECT UNIX_TIMESTAMP(CONVERT_TZ('2024-05-20 15:00:00','UTC','UTC')); -- explicit UTC
SQL (PostgreSQL)
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2024-05-20 15:00:00+00')::bigint; -- seconds
SELECT (EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2024-05-20 15:00:00+00')*1000)::bigint; -- ms
SQL Server (T-SQL)
SELECT DATEDIFF_BIG(SECOND, '1970-01-01T00:00:00Z', SYSUTCDATETIME());
-- For a specific UTC datetime2 value
DECLARE @d datetime2 = '2024-05-20T15:00:00Z';
SELECT DATEDIFF_BIG(SECOND, '1970-01-01T00:00:00', @d);
Excel / Google Sheets
=INT((A1 - DATE(1970,1,1)) * 86400)=INT((A1 - DATE(1970,1,1)) * 86400000)APIs (JSON)
| s/ms |
| As parsed; Z => UTC |
DateTimeOffset |
| MySQL | UNIX_TIMESTAMP() | s | Session TZ unless UTC set | 'YYYY-MM-DD HH:MM:SS' |
| PostgreSQL | EXTRACT(EPOCH ...) | s | Respect TZ type | TIMESTAMP WITH TIME ZONE |
| Excel | ((A1 - DATE(1970,1,1))*86400) | s/ms | Local | Date cell (local) |