Learn how to convert to Unix epoch time accurately across languages and tools. Practical steps, real-world examples, common pitfalls, and best practices from a senior SEO and technical writer.
Converting timestamps is harder than it looks—especially when time zones, daylight saving time, and different precisions get involved. If you’ve ever needed to convert to Unix epoch and found conflicting answers, this guide is for you. We’ll explain the concepts simply, show you reliable code for major languages, and give you best practices that prevent production bugs.
To help you move fast, we’ve included copy‑paste snippets, real‑world use cases, and a step‑by‑step workflow using ZenixTools.
Quick answer (Featured Snippet)
To convert to Unix epoch, parse your date in UTC and return seconds since 1970‑01‑01T00:00:00Z. Examples: JavaScript Math.floor(new Date('2024-08-01T12:30:00Z').getTime()/1000); Python int(datetime.fromisoformat('2024-08-01T12:30:00+00:00').timestamp()); Java Instant.parse('2024-08-01T12:30:00Z').getEpochSecond(); Bash date -ud '2024-08-01 12:30 UTC' +%s; PostgreSQL SELECT EXTRACT(EPOCH FROM TIMESTAMP '2024-08-01 12:30:00+00'). Always use UTC and pick seconds or milliseconds consistently.
AI Overview (for quick scanning)
This guide explains how to convert to Unix epoch (also called Unix time, POSIX time, or epoch time) reliably. You’ll learn what epoch time is, why it matters, and how to convert timestamps in JavaScript, Python, Java, C#, PHP, Ruby, Go, Bash, SQL, Excel/Sheets, and more. We cover common mistakes (UTC vs local time, seconds vs milliseconds), best practices, and real-world examples for logging, APIs, analytics, and databases. Includes a simple step-by-step with ZenixTools.
Unix epoch time (also called Unix timestamp or POSIX time) is a simple number that counts the seconds since midnight at the start of January 1, 1970, in Coordinated Universal Time (UTC). By design, it ignores time zones and daylight saving time. That makes it a universal clock for computers and APIs.
Key points:
Converting to epoch has two steps:
Important choices:
Notes:
Tip: Always standardize to UTC and confirm seconds vs milliseconds.
// From ISO 8601 to epoch seconds
const epochSeconds = Math.floor(new Date('2024-08-01T12:30:00Z').getTime() / 1000);
// If you need milliseconds
authority const epochMs = new Date('2024-08-01T12:30:00Z').getTime();
// From local time with explicit zone using Luxon (recommended)
// npm i luxon
const { DateTime } = require('luxon');
const dt = DateTime.fromISO('2024-08-01T12:30:00', { zone: 'America/New_York' }).toUTC();
const epochSec = Math.floor(dt.toMillis() / 1000);
from datetime import datetime, timezone
# ISO 8601 with offset
epoch_sec = int(datetime.fromisoformat('2024-08-01T12:30:00+00:00').timestamp())
# Naive time interpreted as America/New_York using zoneinfo (Python 3.9+)
from zoneinfo import ZoneInfo
local_dt = datetime(2024, 8, 1, 12, 30, 0, tzinfo=ZoneInfo('America/New_York'))
epoch_sec_local = int(local_dt.timestamp())
# Milliseconds
epoch_ms = int(local_dt.timestamp() * 1000)
import java.time.*;
long epochSec = Instant.parse("2024-08-01T12:30:00Z").getEpochSecond();
ZonedDateTime zdt = ZonedDateTime.of(2024, 8, 1, 12, 30, 0, 0, ZoneId.of("America/New_York"));
long epochMs = zdt.toInstant().toEpochMilli();
using System;
var instant = DateTimeOffset.Parse("2024-08-01T12:30:00Z");
long epochSec = instant.ToUnixTimeSeconds();
long epochMs = instant.ToUnixTimeMilliseconds();
var local = new DateTimeOffset(2024, 8, 1, 12, 30, 0, TimeSpan.FromHours(-4));
long localEpoch = local.ToUnixTimeSeconds();
<?php
$dt = new DateTime('2024-08-01T12:30:00Z');
$epochSec = $dt->getTimestamp();
$local = new DateTime('2024-08-01 12:30:00', new DateTimeZone('America/New_York'));
$epochMs = $local->getTimestamp() * 1000;
time = Time.iso8601('2024-08-01T12:30:00Z')
epoch_sec = time.to_i
time_ny = Time.new(2024,8,1,12,30,0, '-04:00')
epoch_ms = (time_ny.to_f * 1000).to_i
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse(time.RFC3339, "2024-08-01T12:30:00Z")
fmt.Println(t.Unix()) // seconds
fmt.Println(t.UnixMilli()) // milliseconds (Go 1.17+)
}
# UTC input
date -ud '2024-08-01 12:30:00 UTC' +%s
# Local time in America/New_York (if tzdata available)
TZ=America/New_York date -d '2024-08-01 12:30:00' +%s
-- PostgreSQL
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2024-08-01 12:30:00+00'); -- seconds
SELECT (EXTRACT(EPOCH FROM TIMESTAMPTZ '2024-08-01 12:30:00+00') * 1000)::bigint; -- ms
-- MySQL 8+
SELECT UNIX_TIMESTAMP('2024-08-01 12:30:00'); -- seconds UTC if input is UTC
SELECT UNIX_TIMESTAMP(CONVERT_TZ('2024-08-01 12:30:00','America/New_York','+00:00'));
-- SQLite (seconds since epoch)
SELECT strftime('%s','2024-08-01 12:30:00','utc');
# Convert an ISO 8601 UTC value in A2 to epoch seconds
=(A2 - DATE(1970,1,1)) * 86400
# If A2 is local and you know the offset (e.g., -4 hours):
=((A2 - (4/24)) - DATE(1970,1,1)) * 86400
Tip: Excel/Sheets store dates as days since 1899-12-30 (Excel) or 1899-12-30 (Sheets). Always correct for UTC/offsets.
Mixing seconds and milliseconds
Forgetting UTC
Ambiguous input formats
Daylight saving time (DST) surprises
Naive datetimes
Manual math
32-bit overflows
| Method / Choice | Granularity | Pros | Cons | Typical Use |
|---|---|---|---|---|
| Epoch seconds (int) | 1s | Compact, widely supported | Lower precision | Most APIs, SQL filtering |
| Epoch milliseconds (int) | 1ms | Good precision, still compact | Easy to confuse with seconds | Web clients, logs, analytics |
| Epoch micro/nanoseconds | µs/ns | Very high precision | Library/DB support varies | Trading, telemetry |
| Human-readable ISO 8601 | n/a | Readable, unambiguous | Slower to parse, larger | UI, logs for humans |
| Local time + zone | n/a | Contextual for users | DST errors, parsing issues | UI input only |
| Storing as DATETIME | n/a | Built-in functions | Time zone semantics differ | Legacy schemas |
It’s the number of seconds since 1970‑01‑01T00:00:00Z (UTC). It’s a timezone-agnostic integer used by operating systems, databases, and APIs.
Seconds are epoch divided by 1. Milliseconds multiply seconds by 1000. Many JavaScript and web systems prefer milliseconds. Do not mix them.
Parse the ISO string as UTC, then get seconds or milliseconds since the epoch. Use built-in methods shown in this guide for your language.
Attach the correct zone (IANA, like America/New_York) or offset (e.g., -04:00), convert to UTC, then compute epoch.
Usually a local-time vs UTC bug. Ensure you parse with an explicit zone and run conversions in UTC.
DST affects local times but not UTC. Convert local timestamps to UTC first, then to epoch.
POSIX time ignores leap seconds. It treats time as a continuous count. Most libraries follow this convention.
Use BIGINT for seconds or milliseconds. Index the column if you filter by time. Document the precision.
Reverse the process: interpret the integer as seconds or milliseconds since 1970‑01‑01T00:00:00Z, then format in UTC or a target zone.
You’re likely mixing milliseconds with seconds. Divide or multiply by 1000 as needed and rename the field.
Require ISO 8601, or use robust parsers that reject ambiguous formats. Ask for an explicit time zone.
On 32-bit systems, epoch seconds may overflow in 2038. Use 64-bit integers to avoid it.
For machines: epoch (fast, compact). For humans and logs: ISO 8601 in UTC. Many systems keep both.
Yes. Subtract two epoch values for a duration in seconds or milliseconds. Convert to minutes/hours for display.
Use ISO 8601 (RFC 3339 profile) for human-readable and epoch for machine efficiency. Document your choice and precision in the API contract.
Unix epoch is a simple, fast, and reliable way to represent time across systems. Standardize on UTC, pick a precision, and use proven library functions. With these practices and the snippets above, you can confidently convert to Unix epoch in any stack—and avoid the subtle bugs that hurt reliability and analytics.
Ready to move faster? Try ZenixTools’ Convert to Unix Epoch tool to parse, validate, and convert timestamps in seconds—no setup required. Batch-convert logs, switch time zones, and copy results with one click. Build once, ship with confidence.
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.