Master date convert epoch with step-by-step guides, code examples, best practices, and real-world tips. Learn UTC vs local, seconds vs milliseconds, and avoid common pitfalls.
Converting a human-readable date to an epoch timestamp—and back—is a core skill for anyone who works with data, APIs, logs, or automations. If you’ve ever searched for “date convert epoch” while debugging time zone issues or milliseconds vs seconds, this guide is for you.
We’ll explain the concepts in plain language, show you fast, copy-paste code examples, and highlight the pitfalls pros watch for. By the end, you’ll convert confidently in any environment.
To convert a date to epoch (Unix time), use UTC and confirm units (seconds vs milliseconds). Example: JavaScript to seconds: Math.floor(new Date('2024-12-01T12:00:00Z').getTime() / 1000). Python: int(datetime.datetime(2024,12,1,12,0,0,tzinfo=datetime.timezone.utc).timestamp()). To convert epoch to date: JavaScript: new Date(1733054400 * 1000).toISOString(). Always verify the time zone.
This guide explains how to convert dates to and from epoch (Unix time) with reliable, real-world techniques. You’ll learn UTC vs local time, seconds vs milliseconds, and language-specific recipes for JavaScript, Python, Java, PHP, SQL, Bash, C#, Go, and Ruby. We include step-by-step checks, best practices, and common mistakes (DST, leap seconds, and truncation). You’ll also find a comparison table, FAQs, and references to MDN, W3C, and Schema.org. Use ZenixTools to validate conversions instantly and avoid time zone pitfalls in production.
“Epoch” (Unix time) is a numeric count of time since 1970-01-01T00:00:00Z (UTC). Most systems use seconds, though some languages and APIs use milliseconds. “Date convert epoch” simply means translating a human-readable date and time into this numeric count—or reversing the process.
Key points:
Follow these steps to avoid the most common time pitfalls.
Below are reliable, copy-ready snippets. Replace the example date with your own.
Note: Unless stated, examples yield epoch seconds. Multiply/divide when needed.
// Date to epoch seconds (UTC)
const epochSec = Math.floor(new Date('2024-12-01T12:00:00Z').getTime() / 1000);
// Epoch seconds to ISO date (UTC)
const iso = new Date(1733054400 * 1000).toISOString();
Tips:
import datetime as dt
# Date to epoch seconds (UTC)
epoch_sec = int(dt.datetime(2024, 12, 1, 12, 0, 0, tzinfo=dt.timezone.utc).timestamp())
# Epoch seconds to ISO 8601
epoch = 1733054400
iso = dt.datetime.fromtimestamp(epoch, tz=dt.timezone.utc).isoformat().replace('+00:00', 'Z')
Notes:
import java.time.*;
// Date to epoch seconds
Instant instant = Instant.parse("2024-12-01T12:00:00Z");
long epochSec = instant.getEpochSecond();
// Epoch to ISO 8601
Instant fromEpoch = Instant.ofEpochSecond(1733054400);
String iso = fromEpoch.toString(); // UTC with 'Z'
Use java.time (Instant, ZonedDateTime) not legacy java.util.Date when possible.
// Date to epoch seconds (UTC)
$dt = new DateTime('2024-12-01T12:00:00Z');
$epoch = $dt->getTimestamp();
// Epoch to ISO 8601
$iso = (new DateTime('@1733054400'))->setTimezone(new DateTimeZone('UTC'))->format(DATE_ATOM); // 2024-12-01T12:00:00+00:00
Tip: '@<epoch>' creates a DateTime from epoch seconds.
# Date to epoch seconds (UTC)
date -u -d '2024-12-01T12:00:00Z' +%s
# Epoch to ISO 8601 (UTC)
date -u -d @1733054400 +"%Y-%m-%dT%H:%M:%SZ"
Note: macOS uses BSD date. Prefer gdate from coreutils via Homebrew (brew install coreutils).
PostgreSQL:
-- Date to epoch seconds (UTC)
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2024-12-01 12:00:00+00');
-- Epoch to UTC timestamp
SELECT to_timestamp(1733054400) AT TIME ZONE 'UTC';
MySQL/MariaDB:
SELECT UNIX_TIMESTAMP('2024-12-01 12:00:00') AS epoch_sec; -- assumes system_time_zone
SELECT FROM_UNIXTIME(1733054400) AS local_ts; -- convert to session time zone
SQLite (3.38+):
SELECT strftime('%s','2024-12-01 12:00:00','utc'); -- to epoch seconds
SELECT strftime('%Y-%m-%dT%H:%M:%SZ',1733054400,'unixepoch'); -- to ISO UTC
var instant = DateTimeOffset.Parse("2024-12-01T12:00:00Z");
long epochSec = instant.ToUnixTimeSeconds();
var fromEpoch = DateTimeOffset.FromUnixTimeSeconds(1733054400).UtcDateTime.ToString("o");
import (
"time"
)
t, _ := time.Parse(time.RFC3339, "2024-12-01T12:00:00Z")
epochSec := t.Unix()
iso := time.Unix(1733054400, 0).UTC().Format(time.RFC3339)
t = Time.iso8601('2024-12-01T12:00:00Z')
epoch_sec = t.to_i
iso = Time.at(1733054400).utc.iso8601
.toISOString(), ZonedDateTime, strftime('%Y-%m-%dT%H:%M:%SZ').start, end, and expires_at fields.Cache-Control: max-age to an absolute expiration epoch.exp claim uses epoch seconds (UTC).FROM_UNIXTIME and UNIX_TIMESTAMP can be session-dependent.createdAtEpochSec, expiresAtMs.Date.parse(iso) or new Date(iso), not localized strings.tzinfo=timezone.utc to avoid naive datetime pitfalls.Intl.DateTimeFormat with a user’s locale/time zone.| Environment | Date → Epoch (UTC) | Epoch → Date (UTC) | Units Default |
|---|---|---|---|
| JavaScript | Math.floor(new Date(iso).getTime()/1000) | new Date(sec*1000).toISOString() | ms |
| Python | int(dt.datetime(..., tz=UTC).timestamp()) | dt.datetime.fromtimestamp(sec, UTC) | s |
| Java | Instant.parse(iso).getEpochSecond() | Instant.ofEpochSecond(sec).toString() | s |
| PHP | $dt->getTimestamp() | (new DateTime('@sec'))->setTimezone(UTC) | s |
| Bash (GNU date) | date -u -d 'ISO' +%s | date -u -d @sec +ISO | s |
| PostgreSQL | EXTRACT(EPOCH FROM ts) | to_timestamp(sec) AT TIME ZONE 'UTC' | s |
| MySQL | UNIX_TIMESTAMP(ts) |
Notes:
YYYY-MM-DDTHH:mm:ssZ format.What is epoch time? Epoch (Unix time) is the number of seconds elapsed since 1970-01-01T00:00:00Z (UTC), excluding leap seconds.
Is epoch in seconds or milliseconds? Traditionally seconds. JavaScript commonly uses milliseconds. Always check docs and variable names.
How do I convert date to epoch in JavaScript?
Math.floor(new Date('2024-12-01T12:00:00Z').getTime()/1000) returns epoch seconds.
How do I convert epoch to date in JavaScript?
new Date(1733054400 * 1000).toISOString() returns an ISO 8601 UTC string.
Why does my result look 1000x too big/small? You’ve mixed seconds and milliseconds. Adjust by dividing or multiplying by 1000.
Should I store epoch or ISO 8601 in my database? Store epoch for performance and integer operations, or ISO 8601 for readability. Many teams store epoch seconds and compute display strings at read time.
What time zone should I use? Use UTC for storage and transport. Convert to local time only in the UI or final presentation.
How do I avoid DST issues? Operate in UTC for calculations. Only apply local time zones for display or user input, using robust libraries.
Why do my SQL epoch conversions look off by a few hours? Your session or server time zone may differ from UTC. Set and verify time zones explicitly.
How can I validate a timestamp quickly? Use ZenixTools’ Timestamp Converter to cross-check units, time zones, and ISO formatting instantly.
Do leap seconds affect epoch time? Unix time ignores leap seconds. Most systems effectively smear or skip them; don’t expect seconds labeled 60.
Can epoch represent dates before 1970? Yes, as negative numbers. Some tools, however, have limited support.
Converting a date to epoch—and epoch back to a readable date—seems simple until time zones, DST, and unit mismatches enter the picture. With a UTC-first approach, ISO 8601 parsing, and careful checks for seconds vs milliseconds, you can make “date convert epoch” a safe, repeatable operation across every stack you use.
Validate your timestamps now. Open ZenixTools’ Unix Timestamp Converter, paste your date or epoch, toggle UTC/local, and copy the exact value you need. Save time, avoid time zone bugs, and standardize your workflow today.
A complete, human-friendly guide to convert to WebP for faster sites and better SEO. Learn benefits, step-by-step workflows, code examples, and expert tips. Use ZenixTools to convert to WebP in seconds.
A practical, expert guide to convert base64 to string across languages, with steps, examples, pitfalls, and best practices.
FROM_UNIXTIME(sec) |
| s |
| SQLite | strftime('%s', ts,'utc') | strftime(ISO, sec,'unixepoch') | s |
| C# | DateTimeOffset.Parse(iso).ToUnixTimeSeconds() | FromUnixTimeSeconds(sec) | s |
| Go | time.Parse(...).Unix() | time.Unix(sec,0).UTC() | s |
How precise is epoch time? Seconds by default; some systems use milliseconds, microseconds, or nanoseconds. Align precision across services.
How do I convert local time input to epoch? Parse with the user’s time zone -> convert to UTC -> compute epoch seconds.
What’s the safest string format to parse? ISO 8601/RFC 3339 (e.g., 2024-12-01T12:00:00Z). Avoid locale-dependent formats.