Unix Timestamp to Date and Back: A Developer Cheat Sheet
Convert Unix timestamps to dates and back, handle seconds vs milliseconds, UTC, time zones, and common API mistakes.
Quick answer
A Unix timestamp is the number of seconds (or milliseconds) since 1970-01-01 00:00:00 UTC. Convert to a date with new Date(seconds * 1000) in JavaScript when the value is in seconds, or new Date(milliseconds) when it is in ms. Always store and exchange UTC; convert to local time only for display.
Key takeaways
- ›10-digit values are usually seconds; 13-digit values are usually milliseconds.
- ›Unix time is always UTC — time zones apply only when formatting for users.
- ›Document whether your API uses seconds or milliseconds to prevent off-by-1000 bugs.
- ›Use the Timestamp Converter to verify conversions without writing scripts.
Apply this guide with the Timestamp Converter
Open Timestamp ConverterWhat is a Unix timestamp?
A Unix timestamp (also called epoch time) counts the number of time units since 1970-01-01 00:00:00 UTC — the Unix epoch. In most APIs and databases you will see either:
- Seconds —
1705312200(10 digits, common in Linux, PostgreSQL, many REST APIs) - Milliseconds —
1705312200000(13 digits, common in JavaScriptDate.now())
Always confirm which unit a system uses before converting. A value that looks like a valid seconds timestamp can decode to a nonsense date if it was actually milliseconds (or vice versa).
Seconds vs milliseconds
| Unit | Example | Typical sources |
|---|---|---|
| Seconds | 1705312200 | Unix CLI, Python time.time(), many backend APIs |
| Milliseconds | 1705312200000 | JavaScript Date.now(), some mobile SDKs, Elasticsearch |
const seconds = 1705312200;
const fromSeconds = new Date(seconds * 1000);
const millis = 1705312200000;
const fromMillis = new Date(millis);
console.log(fromSeconds.toISOString()); // 2024-01-15T10:30:00.000Z
console.log(fromMillis.toISOString()); // same instant when units matchRule of thumb: 10 digits → seconds; 13 digits → milliseconds.
How to convert Unix timestamp to date
JavaScript
function timestampToDate(value) {
const n = Number(value);
if (!Number.isFinite(n)) throw new Error('Invalid timestamp');
// Auto-detect seconds vs milliseconds
const ms = n < 1e12 ? n * 1000 : n;
return new Date(ms);
}
timestampToDate(1705312200).toISOString();
// "2024-01-15T10:30:00.000Z"Python
from datetime import datetime, timezone
ts_seconds = 1705312200
dt = datetime.fromtimestamp(ts_seconds, tz=timezone.utc)
print(dt.isoformat()) # 2024-01-15T10:30:00+00:00Use the Timestamp Converter to verify conversions interactively without writing code.
How to convert date to Unix timestamp
JavaScript
const date = new Date('2024-01-15T10:30:00Z');
const seconds = Math.floor(date.getTime() / 1000);
const millis = date.getTime();
console.log(seconds); // 1705312200
console.log(millis); // 1705312200000ISO string to timestamp (API payload)
const apiPayload = {
event: 'user_signup',
occurred_at: Math.floor(new Date('2024-01-15T10:30:00Z').getTime() / 1000),
};UTC vs local time
Unix timestamps are always UTC-based. They do not store a time zone. When you display a timestamp:
- Store and transmit in UTC (ISO 8601 with
Zor explicit offset) - Convert to local time only in the UI for the user's locale
const d = new Date(1705312200 * 1000);
// UTC (safe for logs and APIs)
console.log(d.toISOString());
// User's local timezone (display only)
console.log(d.toLocaleString('en-US', { timeZone: 'America/Chicago' }));JavaScript examples
Current Unix time
const nowSeconds = Math.floor(Date.now() / 1000);
const nowMillis = Date.now();Parse API response safely
function parseApiTimestamp(field) {
if (typeof field === 'string' && field.includes('T')) {
return new Date(field); // ISO 8601 string
}
const n = Number(field);
return new Date(n < 1e12 ? n * 1000 : n);
}Compare two events
const a = 1705312200;
const b = 1705312300;
const diffSeconds = b - a; // 100 seconds between eventsAPI and log examples
REST API JSON
{
"order_id": "ord_123",
"created_at": 1705312200,
"updated_at": "2024-01-15T10:35:00Z"
}Best practice: Pick one format per field and document it. Mixing raw Unix seconds and ISO strings in the same API confuses clients.
Log line parsing
2024-01-15T10:30:00.123Z INFO request_id=abc duration_ms=42If your log aggregator stores @timestamp as epoch millis, grep for the wrong unit and events appear hours or years off.
Database query (PostgreSQL)
SELECT to_timestamp(1705312200) AT TIME ZONE 'UTC';
-- 2024-01-15 10:30:00Common timestamp mistakes
- Treating milliseconds as seconds — dates in 1970 or far future
- Using local time in APIs — daylight saving bugs and ambiguous instants
- Storing display strings without timezone —
"01/15/2024 10:30"is ambiguous - Comparing strings instead of instants — sort order breaks
- Ignoring leap seconds — rare but relevant for financial/telemetry systems
- Using
new Date("2024-01-15")— parsed as UTC midnight in ES5, local in some engines; prefer ISO withZ
Best practices for APIs and distributed systems
- Store UTC — Unix seconds or ISO 8601 with offset
- Document the unit — seconds vs milliseconds in OpenAPI/schema
- Use ISO 8601 in JSON when human readability matters; use integers when sorting/range queries dominate
- Never rely on client clock for ordering — use server-generated timestamps
- Include time zone in user preferences — convert at display time only
- Validate ranges — reject timestamps before epoch or unreasonably far in the future
For deeper distributed-systems patterns (clock skew, NTP, logical clocks), see Timestamp Management in Distributed Systems.
Related tools
Related guides
Timestamp Management in Distributed Systems
Best practices for handling timestamps across different timezones and distributed systems.
TimestampsISO 8601 Dates in JSON APIs: Formatting Rules That Prevent Bugs
Use ISO 8601 correctly in JSON APIs: UTC vs offsets, fractional seconds, date-only pitfalls, and Unix timestamp interoperability.