Epoch Format: The Complete, Human-Friendly Guide for Devs, Analysts, and Product Teams
Introduction
Epoch format, also called Unix time, is one of the simplest and most reliable ways to represent time in software. It counts the seconds (or milliseconds) since January 1, 1970 UTC. This guide explains how epoch timestamps work, how to convert them in different languages, how to avoid common mistakes, and when to use other formats like ISO 8601.
Featured Snippet (50–70 words)
Epoch format is a numeric timestamp counting seconds (or milliseconds) since January 1, 1970 UTC. It’s compact, time zone–agnostic, and ideal for storage, sorting, and comparison. To convert: divide milliseconds by 1000 for seconds, or multiply seconds by 1000 for milliseconds. Use epoch for databases, logging, analytics, and caching; use ISO 8601 for human-readable APIs.
AI Overview (under 150 words)
Epoch format (Unix time) represents time as a single integer: seconds or milliseconds since 1970-01-01T00:00:00Z. It’s fast to compare, compact to store, and easy to index. Use it in logs, metrics, events, and databases. Convert with built-in functions in JavaScript, Python, SQL, and more. Watch for common pitfalls: seconds vs milliseconds, local vs UTC, DST assumptions, and 32-bit overflow in legacy systems. Best practice: store in UTC, document units, and expose ISO 8601 externally. Try ZenixTools to convert, format, and validate timestamps.
Key Takeaways
- Epoch is the number of seconds or milliseconds since 1970-01-01 UTC.
- It’s compact, timezone-neutral, and great for sorting and indexing.
- Always document units (s vs ms) and time zone (UTC).
- Store epoch internally; present ISO 8601 (RFC 3339) externally.
- Use built-in conversion helpers in your language or database.
- Beware common pitfalls: ms vs s, DST assumptions, and precision loss.
Table of Contents
- What is epoch format
- Why it Matters
- Benefits
- Step-by-Step Guide
- Real World Examples
- Common Mistakes
- Best Practices
- Expert Tips
- Comparison Table
- Frequently Asked Questions
- External References
- Internal Link Suggestions
- Conclusion
- Call To Action
Epoch format, also known as Unix time, represents a point in time as a single number: the count of seconds since the Unix epoch, which is 1970-01-01 00:00:00 UTC. Many systems also use milliseconds since epoch for higher precision.
Key details:
- Epoch starts at 1970-01-01T00:00:00Z (UTC).
- Two common units:
- Seconds since epoch (10-digit values today, e.g., 1717171717)
- Milliseconds since epoch (13-digit values, e.g., 1717171717000)
- Negative values represent dates before 1970 (supported in many modern systems).
- Epoch doesn’t include a time zone; it’s inherently based on UTC.
Why it Matters
- Universal baseline: A consistent reference point across languages, platforms, and databases.
- Performance: Integers are faster to compare and index than strings.
- Storage efficiency: Smaller footprint than verbose date strings.
- Data interoperability: Common in logs, analytics, metrics, and distributed systems.
- SEO and product analytics: Event ordering, funnel timing, and latency tracking rely on precise, comparable timestamps.
Benefits
- Simple comparisons: Earlier times are smaller numbers.
- Easy sorting and indexing: Databases and search engines optimize numeric fields.
- Compact: Saves space in telemetry, logs, and embedded devices.
- Time zone neutrality: Avoids user locale complexities until presentation.
- Precision control: Use seconds, milliseconds, microseconds, or nanoseconds depending on your stack.
Step-by-Step Guide
This section shows how to detect units, convert between epoch and human-readable dates, and work across popular languages and databases.
1) Identify the unit (seconds vs milliseconds)
- 10 digits (e.g., 1717171717) → likely seconds
- 13 digits (e.g., 1717171717000) → likely milliseconds
- Quick checks:
- If value > 10^11, it’s probably milliseconds
- To safely convert ms → s: floor(epoch_ms / 1000)
2) Convert epoch to a date and back
- From seconds to human date: add seconds to 1970-01-01 UTC
- From milliseconds to human date: divide by 1000, then convert
- From date to epoch: convert the date to UTC and get seconds (or ms) since epoch
3) JavaScript / TypeScript
// Epoch (ms) → Date
const dateFromMs = new Date(1717171717000);
// Epoch (s) → Date
const dateFromSec = new Date(1717171717 * 1000);
// Date → Epoch (ms)
const nowMs = Date.now();
// Date → Epoch (s)
const nowSec = Math.floor(Date.now() / 1000);
// ISO 8601
const iso = new Date().toISOString(); // e.g., "2026-09-06T12:34:56.789Z"
Node.js tip: prefer Date.now() for ms; performance.now() is monotonic for measuring durations (not wall-clock time).
4) Python
from datetime import datetime, timezone
# Epoch (s) → datetime (UTC)
ts_sec = 1717171717
dt = datetime.fromtimestamp(ts_sec, tz=timezone.utc)
# Epoch (ms) → datetime (UTC)
ts_ms = 1717171717000
dt_ms = datetime.fromtimestamp(ts_ms / 1000, tz=timezone.utc)
# datetime → epoch (s)
epoch_s = int(dt.timestamp())
# datetime → epoch (ms)
epoch_ms = int(dt.timestamp() * 1000)
# ISO 8601
iso = dt.isoformat().replace('+00:00', 'Z')
5) Java
import java.time.*;
// Epoch (s) → Instant
long s = 1717171717L;
Instant instant = Instant.ofEpochSecond(s);
// Epoch (ms) → Instant
long ms = 1717171717000L;
Instant instantMs = Instant.ofEpochMilli(ms);
// Instant → epoch
long backToS = instant.getEpochSecond();
long backToMs = instant.toEpochMilli();
// ISO 8601
String iso = instant.toString(); // e.g., 2026-09-06T12:34:56Z
6) Go
package main
import (
"fmt"
"time"
)
func main() {
// Epoch (s) → time.Time
sec := int64(1717171717)
t := time.Unix(sec, 0).UTC()
// Epoch (ms) → time.Time
ms := int64(1717171717000)
tms := time.UnixMilli(ms).UTC()
// time.Time → epoch
s := t.Unix()
msBack := t.UnixMilli()
// ISO 8601
iso := t.Format(time.RFC3339Nano)
fmt.Println(iso, s, msBack, tms)
}
7) PHP
// Epoch (s) → DateTime (UTC)
$sec = 1717171717;
$dt = (new DateTime('@' . $sec))->setTimezone(new DateTimeZone('UTC'));
// Epoch (ms) → DateTime (UTC)
$ms = 1717171717000;
$dtMs = (new DateTime('@' . intval($ms / 1000)))->setTimezone(new DateTimeZone('UTC'));
// DateTime → epoch
$epochS = $dt->getTimestamp();
$epochMs = $epochS * 1000;
// ISO 8601
$iso = $dt->format(DateTime::ATOM); // RFC 3339
8) C# (.NET)
using System;
// Epoch (s) → DateTimeOffset UTC
long s = 1717171717;
var dto = DateTimeOffset.FromUnixTimeSeconds(s);
// Epoch (ms) → DateTimeOffset UTC
long ms = 1717171717000;
var dtoMs = DateTimeOffset.FromUnixTimeMilliseconds(ms);
// Date → epoch
long backS = dto.ToUnixTimeSeconds();
long backMs = dto.ToUnixTimeMilliseconds();
// ISO 8601
string iso = dto.ToUniversalTime().ToString("o"); // 2026-09-06T12:34:56.0000000Z
9) Bash / Shell
# Now → epoch (s)
date +%s
# Now → epoch (ms) (GNU date)
($(date +%s%3N))
# Epoch (s) → human (UTC)
date -u -d @1717171717
# Epoch (ms) → human (UTC)
ms=1717171717000; date -u -d @$(($ms/1000))
10) SQL
PostgreSQL:
-- Epoch (s) → timestamp
SELECT to_timestamp(1717171717) AT TIME ZONE 'UTC';
-- timestamp → epoch (s)
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2026-09-06 12:34:56+00');
MySQL / MariaDB:
-- Epoch (s) → datetime (UTC)
SELECT FROM_UNIXTIME(1717171717);
-- datetime → epoch (s)
SELECT UNIX_TIMESTAMP(UTC_TIMESTAMP());
SQLite:
-- Epoch (s) → ISO 8601 UTC
SELECT datetime(1717171717, 'unixepoch');
-- datetime → epoch (s)
SELECT strftime('%s', '2026-09-06 12:34:56');
BigQuery:
-- Epoch (s) → TIMESTAMP
SELECT TIMESTAMP_SECONDS(1717171717);
-- Epoch (ms) → TIMESTAMP
SELECT TIMESTAMP_MILLIS(1717171717000);
11) Converting time zones for display
- Store in UTC (epoch)
- Convert to user’s local time zone at presentation
Examples:
- JavaScript: new Date(epochMs).toLocaleString('en-US', { timeZone: 'America/New_York' })
- Python: dt.astimezone(ZoneInfo('America/New_York'))
Real World Examples
- Logging and Observability:
- Store event time as epoch for compactness and easy range queries
- Convert to local time in dashboards for readability
- Analytics Pipelines:
- Use epoch to join events across services and time zones
- Windowing in stream processors (e.g., Kafka Streams, Flink) expects numeric timestamps
- Databases and Warehouses:
- Partition tables by epoch day; filter by numeric ranges for speed
- Use epoch in materialized views for faster aggregations
- Caching and Expiry:
- Store TTL or expires_at as epoch for instant comparisons
- IoT and Edge:
- Send epoch to save bytes over constrained networks
- SEO and Web Performance:
- RUM (Real User Monitoring) timestamps in epoch underpin Core Web Vitals analysis
- Blockchain and Ledgers:
- Many systems record block times near-epoch; clients convert to readable forms
Common Mistakes
- Seconds vs milliseconds confusion:
- Symptom: dates are 1970 or far in the future
- Fix: document units; validate length; use strict typing when available
- Assuming local time in storage:
- Epoch is UTC-based; never store local time offsets with it
- DST assumptions:
- Conversions to local time can shift an hour around DST changes; always do final rendering with a robust time zone database
- Precision loss in floats:
- Avoid floating-point for epoch; use integers for seconds and milliseconds
- 32-bit overflow (Year 2038 problem):
- Legacy 32-bit systems may fail; use 64-bit integers
- Negative epochs:
- Dates before 1970 need negative values; ensure your stack supports them
- Truncation on database import:
- Using INT where BIGINT is needed can cut off milliseconds
- Mixing wall-clock and monotonic clocks:
- Don’t compute durations with wall-clock; use monotonic sources (e.g., performance.now())
Best Practices
- Store timestamps in UTC as epoch in internal systems
- Document units explicitly: epoch_seconds or epoch_milliseconds
- Expose ISO 8601 (RFC 3339) in public APIs:
- e.g., 2026-09-06T12:34:56Z
- Use BIGINT for milliseconds in databases
- Keep created_at and updated_at as epoch for indexing; provide computed views as ISO 8601
- Validate inputs at API boundaries; reject invalid lengths or ranges
- Use the IANA time zone database for accurate local conversions
- For analytics, precompute day/hour buckets for speed
Expert Tips
- Rounding strategy:
- Use floor for ms → s to avoid rounding into the future
- For alignment (e.g., minute buckets), zero-out smaller units
- Idempotency keys:
- Combine epoch with a unique ID to deduplicate events
- High-precision needs:
- Some stacks support microseconds/nanoseconds (e.g., Go’s time, PostgreSQL’s TIMESTAMP(6)); confirm end-to-end support
- Latency measurement:
- Use monotonic clocks for durations; convert to wall-clock only when needed
- NTP and clock drift:
- Ensure servers sync time (e.g., chrony, systemd-timesyncd) to avoid skew in distributed systems
- Schema design:
- Keep epoch as a numeric column; add a generated ISO string column for convenience
- Security logs:
- Store both event time and ingestion time (two separate epoch fields) for forensics
Comparison Table
| Format | Example | Pros | Cons | Best Use |
|---|
| Epoch (seconds) | 1717171717 | Compact, fast comparisons, widely supported | Lower precision; s vs ms confusion | Logs, IDs, coarse metrics |
| Epoch (milliseconds) | 1717171717000 | Higher precision, still compact | Larger values; requires BIGINT | Analytics, UI events, detailed telemetry |
| ISO 8601 (RFC 3339) | 2026-09-06T12:34:56Z | Human-readable, time zone explicit, API-friendly | Longer strings, slower to parse | External APIs, exports, reports |
| Human-readable local | Sep 6, 2026 08:34:56 EDT | User friendly | Ambiguous without zone, locale-variant | Final UI display only |
Frequently Asked Questions
- What is epoch format used for?
- Storing, sorting, and comparing timestamps efficiently across systems, logs, and databases.
- Is epoch in seconds or milliseconds?
- Both exist. Seconds (10 digits) and milliseconds (13 digits). Always document which you use.
- How do I convert epoch to a date in JavaScript?
- Use new Date(epochMs) for milliseconds or new Date(epochSec * 1000) for seconds.
- How do I get the current epoch time?
- JS: Date.now() (ms). Python: int(datetime.now(tz=UTC).timestamp()) (s). Linux: date +%s (s).
- Why are my dates showing 1970?
- You likely passed seconds where milliseconds were expected (or vice versa). Adjust by 1000.
- Should I use epoch or ISO 8601 in my API?
- Use ISO 8601 (RFC 3339) for public APIs; use epoch internally for performance.
- How do I handle time zones with epoch?
- Store UTC in epoch. Convert to time zones only when displaying to users.
- What about daylight saving time (DST)?
- Epoch is UTC-based, so it’s unaffected. Convert to local times using a robust TZ database.
- Can epoch represent dates before 1970?
- Yes, with negative values. Confirm your stack supports it.
- What is the Year 2038 problem?
- 32-bit epoch seconds overflow in 2038. Use 64-bit integers to avoid it.
- How do I check if my epoch is seconds or milliseconds?
- Count digits: ~10 digits = seconds; ~13 digits = milliseconds. Or compare range to current time.
- Is epoch affected by leap seconds?
- Unix time typically ignores leap seconds; many systems smear or adjust. Treat epoch as continuous seconds.
- How do I store epoch in SQL?
- Use BIGINT for ms. Add indexes for range queries. Optionally add generated ISO columns.
- Can I sort by epoch to get newest first?
- Yes. Larger numbers are later times. Add DESC order for newest-first.
- What’s the difference between epoch and timestamp fields?
- Epoch is a numeric count since 1970. Timestamp fields may store structured date-time with or without time zone.
External References
Internal Link Suggestions
- ZenixTools Unix Timestamp Converter (convert epoch seconds/milliseconds to date and back)
- ZenixTools ISO 8601 Formatter & Validator (normalize and validate RFC 3339)
- ZenixTools Time Zone Converter (UTC ↔ local time with IANA zones)
- ZenixTools Date Math & Rounding (bucket timestamps by minute/hour/day)
- ZenixTools Log Timestamp Parser (auto-detect format and parse to epoch)
Conclusion
Epoch format is a simple, reliable, and fast way to represent time. Use epoch (seconds or milliseconds) for storage, indexing, and comparisons, and convert to ISO 8601 for external APIs and user-facing content. By documenting units, storing UTC, and following best practices, you can avoid common pitfalls and keep your data consistent across systems. When you need a quick conversion or validation, ZenixTools makes working with epoch format effortless.
Call To Action
Need to convert or validate timestamps right now? Open ZenixTools’ Unix Timestamp Converter to transform epoch seconds or milliseconds into readable dates (and back), format as ISO 8601, and compare time zones—fast, accurate, and built for developers and analysts.