Learn how to convert epoch to Unix time and back with clear steps, code examples, and best practices. Ideal for developers, analysts, and SREs using timestamps.
Introduction
Time formats can be confusing until you understand the basics. If you’ve ever wondered how to convert epoch to unix (and what the difference is), this guide is for you. We’ll explain the concepts in plain English, show quick steps, and share practical code examples in popular languages.
Featured Snippet (50–70 words)
To convert epoch to Unix time: if your epoch value is in seconds since 1970-01-01 UTC, it is already Unix time. If it’s in milliseconds, divide by 1,000 and round down. To convert a human date to Unix, parse the date in UTC and compute seconds since 1970-01-01 00:00:00. Always confirm units (seconds vs milliseconds) and timezone (UTC).
Key Takeaways
Table of Contents
What is epoch to unix
Let’s clear up the terms first.
So, “epoch to unix” usually means:
Key detail: Many APIs and databases store timestamps in milliseconds since the epoch, not seconds. That’s where confusion starts.
Why it Matters
Benefits
Step-by-Step Guide
Follow these steps to convert correctly every time.
Language Examples
JavaScript (Node.js/Browser)
// Date to Unix seconds (UTC)
const date = new Date('2025-01-01T00:00:00Z');
const unixSeconds = Math.floor(date.getTime() / 1000);
// Epoch ms to Unix seconds
const ms = 1704067200000;
const unixFromMs = Math.floor(ms / 1000);
// Unix seconds to Date
const fromUnix = new Date(unixSeconds * 1000); // JS uses ms internally
Python (3.x)
from datetime import datetime, timezone
# Date to Unix seconds (UTC)
dt = datetime(2025, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
unix_seconds = int(dt.timestamp())
# Epoch ms to Unix seconds
ms = 1704067200000
unix_from_ms = ms // 1000
# Unix seconds to datetime (UTC)
from_unix = datetime.fromtimestamp(unix_seconds, tz=timezone.utc)
Bash (GNU date)
# Date string to Unix seconds (UTC)
date -u -d '2025-01-01 00:00:00' +%s
# Unix seconds to formatted UTC date
date -u -d @1704067200 '+%Y-%m-%dT%H:%M:%SZ'
Java
import java.time.*;
// Date to Unix seconds (UTC)
Instant instant = Instant.parse("2025-01-01T00:00:00Z");
long unixSeconds = instant.getEpochSecond();
// Epoch ms to Unix seconds
long ms = 1704067200000L;
long unixFromMs = ms / 1000L;
// Unix seconds to Instant
Instant fromUnix = Instant.ofEpochSecond(unixSeconds);
PHP
// Date to Unix seconds (UTC)
$dt = new DateTime('2025-01-01T00:00:00Z');
$unixSeconds = $dt->getTimestamp();
// Epoch ms to Unix seconds
$ms = 1704067200000;
$unixFromMs = intdiv($ms, 1000);
// Unix seconds to DateTime (UTC)
$fromUnix = (new DateTime('@' . $unixSeconds))->setTimezone(new DateTimeZone('UTC'));
Go
package main
import (
"fmt"
"time"
)
func main() {
// Date to Unix seconds (UTC)
t, _ := time.Parse(time.RFC3339, "2025-01-01T00:00:00Z")
unixSeconds := t.Unix()
// Epoch ms to Unix seconds
ms := int64(1704067200000)
unixFromMs := ms / 1000
// Unix seconds to time.Time
fromUnix := time.Unix(unixSeconds, 0).UTC()
fmt.Println(unixSeconds, unixFromMs, fromUnix)
}
SQL (PostgreSQL)
-- Date to Unix seconds (UTC)
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2025-01-01 00:00:00+00')::bigint AS unix_seconds;
-- Unix seconds to timestamp
SELECT to_timestamp(1704067200) AT TIME ZONE 'UTC' AS utc_ts;
-- Milliseconds to timestamp
SELECT to_timestamp(1704067200000 / 1000.0) AT TIME ZONE 'UTC' AS utc_ts;
Real World Examples
Common Mistakes
Best Practices
Expert Tips
Comparison Table
| Topic | Unix Seconds (s) | Epoch Milliseconds (ms) | ISO 8601/RFC 3339 | Human-Readable Date |
|---|---|---|---|---|
| Primary Use | Storage, APIs | High-precision events | Interchange, logs | UI/Reports |
| Example | 1704067200 | 1704067200000 | 2025-01-01T00:00:00Z | 2025-01-01 00:00:00 UTC |
| Precision | 1 sec | 1 ms | Variable (to ns) | N/A |
| Pros | Compact, universal | Finer granularity | Timezone explicit | Easy to read |
| Cons | Coarse granularity | Larger numbers | String parsing cost | Ambiguity w/o TZ |
| Conversion | s = floor(ms/1000) | ms = s*1000 | parse/format | parse/format |
Frequently Asked Questions
The Unix epoch is the zero point in Unix time: 1970-01-01 00:00:00 UTC. Unix timestamps measure seconds since this instant, ignoring leap seconds.
Not exactly. The epoch is the starting moment. Unix time is the count of seconds since the epoch. People often say “epoch time” to mean Unix time.
Divide by 1,000 and floor the result. Example: 1704067200000 ms → 1704067200 s.
Check the length. Seconds are usually 10 digits; milliseconds are usually 13 digits for contemporary dates. Also sanity check by converting back to a human date.
Yes. Always parse and compute in UTC to avoid timezone offsets and daylight saving issues.
POSIX/Unix time ignores leap seconds, so 23:59:60 is treated as 23:59:59 repeating. Most apps can safely ignore leap seconds; high-precision systems may need specialized handling.
Only on 32-bit systems using signed 32-bit time_t. Modern 64-bit systems and languages support timestamps far beyond 2038.
Use: Math.floor(new Date('2025-01-01T00:00:00Z').getTime() / 1000). Ensure the date string has a timezone (Z for UTC).
Create a date object from seconds (multiply by 1,000 where needed) and format using your language’s date formatting utilities, ideally in UTC or a known timezone.
Typically yes for seconds. But some systems use floating-point seconds or store separate fields for sub-second precision (ms, µs, ns).
Prefer integers (BIGINT for ms or seconds). They’re compact, index well, and are easy to compare. Convert to strings only for display.
ISO 8601 with UTC (RFC 3339 variant), e.g., 2025-01-01T00:00:00Z. It’s human-readable and unambiguous.
No. Unix timestamps are timezone-agnostic (UTC-based). You must know or specify the intended timezone for display.
Divide by 1,000,000 (µs) or 1,000,000,000 (ns) and floor for integer seconds. Preserve sub-second parts if needed.
Common causes: wrong unit (ms vs s), parsing in local time instead of UTC, rounding errors, or an invalid input date string.
External References
Related Tools from ZenixTools
AI Overview (Quick Summary)
Epoch is the fixed starting point: 1970-01-01 00:00:00 UTC. Unix time counts seconds since that instant. To convert epoch to Unix: if you have milliseconds, divide by 1,000; if you have seconds, you already have Unix time. Always use UTC, confirm units (10-digit seconds vs 13-digit milliseconds), and prefer ISO 8601 for display. Use built-in language functions to avoid errors.
Conclusion
Converting epoch to Unix becomes simple once you know the rules: identify units, use UTC, convert carefully, and validate results. With the steps and examples above, you can confidently handle timestamps across logs, APIs, databases, and dashboards.
Call To Action
Ready to convert faster? Try ZenixTools’ free Unix Timestamp Converter to convert epoch to unix, milliseconds to seconds, and dates to ISO 8601 in one place. Keep your time data precise, portable, and production-ready.
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.
Master converting from kilometers to miles with exact formulas, quick mental math, charts, and real examples. Written for travelers, runners, students, and pros.