Learn how to convert to epoch (Unix time) with simple steps, code snippets, best practices, and real-world examples. Optimized for Featured Snippets and AI Overviews.
Introduction
If you work with logs, APIs, databases, or scheduling, you’ll often need to convert to epoch—also called Unix time. This guide explains what epoch time is, why it matters, and the fastest ways to convert human-readable dates to epoch (and back). You’ll get step-by-step instructions, code snippets in popular languages, and pro tips to avoid common time bugs.
Featured Snippet (Quick Answer)
Epoch time (Unix time) is the number of seconds that have passed since 1970-01-01 00:00:00 UTC, not counting leap seconds. To convert to epoch, parse the date in UTC and return seconds (or milliseconds) since the Unix epoch. Example in JavaScript: Math.floor(new Date('2024-01-01T00:00:00Z').getTime()/1000). Use ZenixTools’ online converter to avoid timezone and unit errors.
AI Overview (Quick Summary)
Epoch time, or Unix time, counts seconds from 1970-01-01T00:00:00Z. It’s universal, compact, and ideal for logs, APIs, and databases. To convert to epoch, parse the date in UTC and output seconds (or milliseconds) since the epoch. This guide shows conversions using ZenixTools, JavaScript, Python, SQL, Bash, Java, Go, PHP, and C#, explains seconds vs milliseconds, time zones, DST, and leap second nuances, and shares best practices and common pitfalls.
Key Takeaways
Table of Contents
Epoch time—often called Unix time or POSIX time—is a simple integer that counts seconds from the Unix epoch: 1970-01-01 00:00:00 UTC. It ignores leap seconds for simplicity and speed. Many systems also use milliseconds (ms) or even nanoseconds for higher precision.
Converting to epoch means turning a human-readable date (like 2026-08-19 15:23:00-04:00) into a single integer representing seconds (or ms) since the epoch. Converting from epoch means reversing that integer back to a date/time in a selected timezone.
Notes
Below are foolproof ways to convert to epoch—and to convert epoch back to a readable date.
Tips
date -u -j -f "%Y-%m-%dT%H:%M:%SZ" "2026-08-19T13:30:00Z" +%s
date -u -d "2026-08-19T13:30:00Z" +%s
date +%s
date -u -d @1690000000
Warning
date flags differ across distros and macOS. Check man date for your system.Convert date to epoch seconds (UTC):
// ISO 8601 to epoch seconds
const epochSec = Math.floor(new Date('2026-08-19T13:30:00Z').getTime() / 1000);
// Local time to epoch seconds (assumes local timezone)
const epochLocal = Math.floor(new Date('2026-08-19 13:30:00').getTime() / 1000);
// Current time in ms and s
const nowMs = Date.now();
const nowSec = Math.floor(nowMs / 1000);
Epoch to ISO string:
const iso = new Date(1690000000 * 1000).toISOString(); // "2023-07-22T...Z"
Using Luxon (handles timezones more safely):
import { DateTime } from 'luxon';
const dt = DateTime.fromISO('2026-08-19T13:30:00', { zone: 'America/New_York' });
const epochSec = Math.floor(dt.toMillis() / 1000);
from datetime import datetime, timezone
# ISO date in UTC to epoch seconds
epoch_sec = int(datetime.fromisoformat('2026-08-19T13:30:00+00:00').timestamp())
# Local naive time to epoch (treat as local):
# Better: always attach timezone
epoch_local = int(datetime(2026, 8, 19, 13, 30).astimezone().timestamp())
# Now (UTC)
now_epoch = int(datetime.now(tz=timezone.utc).timestamp())
# Epoch to ISO
iso = datetime.fromtimestamp(1690000000, tz=timezone.utc).isoformat()
With pytz/zoneinfo for specific zones:
from datetime import datetime
from zoneinfo import ZoneInfo
dt = datetime(2026, 8, 19, 13, 30, tzinfo=ZoneInfo('America/New_York'))
epoch_sec = int(dt.timestamp())
-- date to epoch seconds (UTC)
SELECT EXTRACT(EPOCH FROM TIMESTAMPTZ '2026-08-19 13:30:00+00')::bigint;
-- epoch seconds to timestamptz
SELECT to_timestamp(1690000000) AT TIME ZONE 'UTC';
-- date to epoch seconds (assumes datetime is in UTC or convert_tz first)
SELECT UNIX_TIMESTAMP('2026-08-19 13:30:00');
-- epoch to datetime
SELECT FROM_UNIXTIME(1690000000);
SELECT strftime('%s', '2026-08-19 13:30:00'); -- seconds since epoch (assumes UTC)
SELECT datetime(1690000000, 'unixepoch');
SELECT UNIX_SECONDS(TIMESTAMP('2026-08-19 13:30:00+00'));
SELECT TIMESTAMP_SECONDS(1690000000);
import java.time.*;
long epochSec = Instant.parse("2026-08-19T13:30:00Z").getEpochSecond();
ZonedDateTime zdt = ZonedDateTime.of(2026, 8, 19, 13, 30, 0, 0, ZoneId.of("America/New_York"));
long epochFromZone = zdt.toEpochSecond();
Instant back = Instant.ofEpochSecond(1690000000);
String iso = back.toString();
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse(time.RFC3339, "2026-08-19T13:30:00Z")
fmt.Println(t.Unix()) // seconds
fmt.Println(t.UnixMilli()) // milliseconds
// Back to time
t2 := time.Unix(1690000000, 0).UTC()
fmt.Println(t2.Format(time.RFC3339))
}
$dt = new DateTime('2026-08-19T13:30:00Z');
echo $dt->getTimestamp(); // seconds
echo (new DateTime('@1690000000'))->setTimezone(new DateTimeZone('UTC'))->format(DateTime::ATOM);
var dt = DateTimeOffset.Parse("2026-08-19T13:30:00Z");
long epochSec = dt.ToUnixTimeSeconds();
var fromEpoch = DateTimeOffset.FromUnixTimeSeconds(1690000000).UtcDateTime;
=TEXT((A2/86400) + DATE(1970,1,1), "yyyy-mm-dd hh:mm:ss")
=TEXT((A2/86400) + DATE(1970,1,1), "yyyy-mm-dd hh:mm:ss")
Note: These assume UTC if the sheet/timezone is set accordingly.
BIGINT epoch for quick ORDER BY and BETWEEN queries.Example: Rolling a 7-day window in SQL (PostgreSQL):
SELECT *
FROM events
WHERE event_epoch BETWEEN EXTRACT(EPOCH FROM now() - interval '7 days')
AND EXTRACT(EPOCH FROM now());
Date.now() gives ms; in many backends, epoch functions return seconds—be explicit.Below compares common timestamp units and typical use cases.
| Unit | Scale | Typical Languages/Functions | Pros | Cons | Use Cases |
|---|---|---|---|---|---|
| Seconds (s) | 1 s | date +%s, Postgres EXTRACT(EPOCH), Java Instant.getEpochSecond() | Compact, great for storage and indexing | Coarse for sub-second events | Logs, scheduling, coarse analytics |
| Milliseconds (ms) | 1e-3 s | JS Date.now(), Go UnixMilli(), Python time.time()*1000 | Good precision, widely supported | Larger integers | UI events, API payloads, general apps |
| Nanoseconds (ns) | 1e-9 s | Go time.Unix(0, ns), some tracing systems | High precision | Not universally supported, very large integers | Tracing, perf profiling, HFT |
Math.floor(new Date('2026-08-19T13:30:00Z').getTime()/1000) for seconds. Use Date.now() for current ms.new Date(epoch*1000), Python datetime.fromtimestamp(epoch, tz=UTC), SQL to_timestamp(epoch).2026-08-19T13:30:00Z.int(datetime.fromisoformat('2026-08-19T13:30:00+00:00').timestamp()) for seconds. Or use zoneinfo for specific zones.EXTRACT(EPOCH FROM timestamptz). MySQL: UNIX_TIMESTAMP(). SQLite: strftime('%s', ...). BigQuery: UNIX_SECONDS().=TEXT((A2/86400)+DATE(1970,1,1),"yyyy-mm-dd hh:mm:ss") where A2 is epoch seconds.Epoch time is a fast, universal way to represent moments in UTC. When you convert to epoch correctly—mindful of timezones and units—you get reliable storage, sorting, and data exchange across systems. Use seconds for compact storage or milliseconds for precision, and favor ISO 8601 for communication. With the right tools and patterns, timestamp work becomes simple and safe.
Ready to convert to epoch without mistakes? Open the ZenixTools Convert to Epoch tool, choose your timezone and unit, and convert instantly. Then try our reverse converter to format epoch back to readable dates. Keep your pipelines clean and consistent—convert to epoch the easy way.
Learn how to convert 1 meter to feet with precise formulas, quick methods, and real-world examples. Includes best practices, common mistakes, comparison tables, FAQs, and expert tips for accurate length conversions.
Learn how to convert 1 meter to feet with the exact formula, step-by-step instructions, quick mental math, and real-world examples. Includes charts, best practices, FAQs, and expert tips.