Learn how to convert epoch to timestamp in seconds or milliseconds, handle time zones, avoid common mistakes, and use simple code in JavaScript, Python, SQL, and more. Includes quick steps, examples, a comparison table, and best practices.
If you work with logs, APIs, or databases, you’ll often need to convert epoch to timestamp. This guide explains the difference between Unix epoch time and human-readable timestamps, shows quick ways to convert them, and helps you avoid common mistakes like seconds vs. milliseconds or time zone drift. Use these steps, examples, and best practices to get accurate results fast.
Quick answer (Featured Snippet): To convert epoch to timestamp, first detect units: 10 digits = seconds, 13 digits = milliseconds. Treat epoch as UTC. Convert using an online converter, or code: in JavaScript, new Date(SECONDS*1000) for seconds, new Date(MILLISECONDS) for ms. Format using toISOString() or strftime-style functions. Adjust for time zones only when displaying to users.
AI Overview: Epoch (Unix time) counts seconds since 1970-01-01T00:00:00Z. A timestamp is the readable date-time string (often ISO 8601). To convert epoch to timestamp: detect if your value is seconds or milliseconds, convert to a date object, then format. Always handle UTC and time zones carefully. This guide covers online tools, code in popular languages, real-world scenarios, pitfalls, and a comparison table for quick reference.
“Convert epoch to timestamp” means transforming a numeric Unix time value (epoch) into a human-readable date-time string. Epoch (also called Unix time or POSIX time) represents the number of seconds (or milliseconds) since 1970-01-01T00:00:00Z (UTC).
You’ll see epoch in logs, analytics events, IoT data, JWT claims, and many APIs. Timestamps are for display, reporting, and cross-system data exchange.
Follow these steps to reliably convert epoch to timestamp and avoid common mistakes.
Tip: Divide by 1000 to go from ms to s. Multiply by 1000 to go from s to ms.
Epoch counts time from 1970-01-01T00:00:00Z. Always interpret it as UTC first. Display in the user’s time zone only at presentation time.
Note: ZenixTools handles large integers (BigInt), negative epochs (pre-1970), and DST boundaries.
JavaScript (Node.js, Browser):
const epochSec = 1700000000;
const d = new Date(epochSec * 1000);
console.log(d.toISOString()); // 2023-11-14T22:13:20.000Z
const epochMs = 1700000000000;
const d = new Date(epochMs);
console.log(d.toISOString());
console.log(d.toLocaleString());
Python (datetime):
import datetime
# Seconds
print(datetime.datetime.utcfromtimestamp(1700000000).isoformat() + 'Z')
# Milliseconds
print(datetime.datetime.utcfromtimestamp(1700000000000 / 1000).isoformat() + 'Z')
Bash (GNU date):
# Seconds
date -u -d @1700000000 +"%Y-%m-%dT%H:%M:%SZ"
# Milliseconds (divide)
ms=1700000000000; date -u -d @$(($ms/1000)) +"%Y-%m-%dT%H:%M:%SZ"
PowerShell:
# Seconds
$epoch = 1700000000
(Get-Date 1970-01-01Z).AddSeconds($epoch).ToString("o")
# Milliseconds
$epochMs = 1700000000000
(Get-Date 1970-01-01Z).AddMilliseconds($epochMs).ToString("o")
PHP:
// Seconds
$epoch = 1700000000;
echo gmdate('c', $epoch); // ISO 8601 in UTC
// Milliseconds
$ms = 1700000000000;
echo gmdate('c', intval($ms/1000));
Java (java.time):
import java.time.*;
import java.time.format.DateTimeFormatter;
// Seconds
Instant instSec = Instant.ofEpochSecond(1700000000L);
System.out.println(instSec.toString()); // ISO-8601 UTC
// Milliseconds
Instant instMs = Instant.ofEpochMilli(1700000000000L);
System.out.println(instMs.toString());
Go:
package main
import (
"fmt"
"time"
)
func main() {
sec := int64(1700000000)
t1 := time.Unix(sec, 0).UTC()
fmt.Println(t1.Format(time.RFC3339))
ms := int64(1700000000000)
t2 := time.Unix(0, ms*int64(time.Millisecond)).UTC()
fmt.Println(t2.Format(time.RFC3339))
}
C# (.NET):
var sec = 1700000000L;
var dt = DateTimeOffset.FromUnixTimeSeconds(sec).UtcDateTime;
Console.WriteLine(dt.ToString("o"));
var ms = 1700000000000L;
var dt2 = DateTimeOffset.FromUnixTimeMilliseconds(ms).UtcDateTime;
Console.WriteLine(dt2.ToString("o"));
Ruby:
# Seconds
puts Time.at(1700000000).utc.iso8601
# Milliseconds
puts Time.at(1700000000000 / 1000.0).utc.iso8601
Swift:
import Foundation
let sec: TimeInterval = 1700000000
let dateSec = Date(timeIntervalSince1970: sec)
print(ISO8601DateFormatter().string(from: dateSec))
let ms: Double = 1700000000000
let dateMs = Date(timeIntervalSince1970: ms / 1000)
print(ISO8601DateFormatter().string(from: dateMs))
SQL (PostgreSQL):
-- Seconds
to_timestamp(1700000000) AT TIME ZONE 'UTC';
-- Milliseconds
to_timestamp(1700000000000 / 1000.0) AT TIME ZONE 'UTC';
SQL (MySQL/MariaDB):
-- Seconds
FROM_UNIXTIME(1700000000);
-- Milliseconds
FROM_UNIXTIME(1700000000000 / 1000);
SQLite:
-- Seconds
datetime(1700000000, 'unixepoch');
-- Milliseconds
datetime(1700000000000 / 1000, 'unixepoch');
Prefer ISO 8601 (e.g., 2024-07-10T14:30:00Z) for APIs and storage. When displaying to users, localize using the appropriate locale and time zone.
Double-check tricky values (DST transitions, pre-1970 times, far-future dates) with a second tool or language.
| Language/Tool | Seconds Conversion | Milliseconds Conversion | Default Zone | ISO 8601 Output Easy? |
|---|---|---|---|---|
| JavaScript | new Date(sec*1000) | new Date(ms) | Local (object holds UTC) | Yes (toISOString) |
| Python | datetime.utcfromtimestamp(sec) | ...(/1000) | UTC if specified | Yes (isoformat) |
| Bash (date) | date -u -d @sec | @$(($ms/1000)) | UTC with -u | Yes (+"%Y-%m-%dT%H:%M:%SZ") |
| PowerShell | AddSeconds(sec) | AddMilliseconds(ms) | UTC if base is Z | Yes (ToString("o")) |
| PHP | gmdate('c', sec) | gmdate('c', ms/1000) | UTC with gmdate | Yes |
| Java | Instant.ofEpochSecond | Instant.ofEpochMilli | UTC Instant | Yes (Instant.toString) |
What is epoch time? Epoch (Unix time) is the number of seconds since 1970-01-01T00:00:00Z, not counting leap seconds. Some systems use milliseconds.
How do I tell if my epoch is seconds or milliseconds? Count digits. 10 digits usually means seconds. 13 digits usually means milliseconds. Very large values are often milliseconds.
How do I convert epoch to timestamp in JavaScript? Use new Date(sec*1000) for seconds or new Date(ms) for milliseconds. Format with toISOString() or toLocaleString().
How do I convert epoch to timestamp in Python? Use datetime.datetime.utcfromtimestamp(seconds). For milliseconds, divide by 1000 first, then format with isoformat() + 'Z'.
Why does my converted time look off by several hours? You likely displayed local time instead of UTC, or mixed units. Validate UTC handling and check seconds vs. milliseconds.
What is the difference between epoch and a timestamp? Epoch is a numeric count from a fixed start (1970-01-01 UTC). A timestamp is a human-readable date-time, often ISO 8601.
Is epoch affected by time zones? No. Epoch is based on UTC. Time zones matter only when formatting for display.
How do I handle daylight saving time (DST)? Store and convert in UTC. When displaying, use a zone-aware formatter that knows DST rules (e.g., America/New_York).
What is the 2038 problem? 32-bit signed integers for epoch seconds overflow around 2038-01-19. Use 64-bit integers or language types like Instant.
How do I convert epoch to ISO 8601? Create a date object from epoch in UTC, then format to ISO 8601 (e.g., toISOString, isoformat, RFC3339). Many languages support this directly.
Can I convert negative epochs (before 1970)? Yes, most modern languages and tools support negative epochs. Verify with a second tool if precision matters.
Converting epoch to timestamp is simple once you detect the units, treat the value as UTC, and use a reliable formatter. Whether you prefer an online tool or code in JavaScript, Python, SQL, or others, follow the best practices here to avoid off-by-hours errors, DST gotchas, and unit mix-ups. When in doubt, verify results with a second source and always standardize on ISO 8601.
Ready to convert epoch to timestamp accurately—every time? Try the ZenixTools Epoch Converter for instant, timezone-aware results. Then bookmark it for logs, APIs, JWTs, and analytics. Explore more time utilities to format, compare, and validate date-time values across your stack.
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.
| Go | time.Unix(sec,0).UTC() | time.Unix(0, ms*1e6).UTC() | UTC if set | Yes (RFC3339) |
| C# | FromUnixTimeSeconds | FromUnixTimeMilliseconds | UTC | Yes (o-format) |
| Ruby | Time.at(sec).utc | Time.at(ms/1000.0).utc | UTC if .utc | Yes (iso8601) |
| PostgreSQL | to_timestamp(sec) | to_timestamp(ms/1000) | Server TZ (use AT TIME ZONE 'UTC') | Yes |
| MySQL | FROM_UNIXTIME(sec) | FROM_UNIXTIME(ms/1000) | Server TZ | Yes (convert to ISO) |
| SQLite | datetime(sec,'unixepoch') | datetime(ms/1000,'unixepoch') | UTC with modifier | Yes |
How do I convert timestamps back to epoch? Parse the timestamp as UTC, then use language functions like getTime()/1000 (JS), datetime.timestamp() (Python), or UNIX_TIMESTAMP() (MySQL).
Are leap seconds included in epoch? Unix time ignores leap seconds. Don’t add or subtract leap seconds manually unless your system specifically requires it.
What format should I use for APIs? Use ISO 8601/RFC 3339 in UTC (e.g., 2024-01-01T12:00:00Z). It’s unambiguous and widely supported.
How do I convert many epochs at once? Use a batch-friendly tool or script (e.g., Python, Node.js) or ZenixTools bulk converter if available. Normalize units first.