Unix Timestamps: A Complete Developer Guide
What Unix timestamps are, why APIs use them, and how to convert between seconds, milliseconds, and human-readable dates.
If you've ever looked at a database and seen a number like 1735689600 instead of a date, you've encountered a Unix timestamp. It's the heartbeat of modern computing, but it can be confusing for beginners.
What is the Unix Epoch?
Unix time is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix Epoch, which is 00:00:00 UTC on 1 January 1970.
Why 1970? It is a convention set by the early Unix developers at Bell Labs, and nothing about that date is physically special. What matters is that every system agrees on the same starting point.
The Year 2038 Problem
Where the timestamp is stored in a signed 32-bit integer, the maximum value it can hold corresponds to 03:14:07 UTC on January 19, 2038. One second later the counter overflows and wraps around to December 13, 1901.
Most modern systems use 64-bit integers, which pushes the overflow date roughly 292 billion years into the future, so it is not a practical concern.
Handling Timestamps in Code
JavaScript
// Get current timestamp (milliseconds)
const now = Date.now();
// Convert to seconds
const unix = Math.floor(Date.now() / 1000);Python
import time
unix = int(time.time())Seconds vs. Milliseconds
There's an important distinction to understand:
- Unix Timestamp (seconds): The traditional format, counting seconds since epoch. Example:
1735689600 - JavaScript Timestamp (milliseconds): JavaScript's
Date.now()returns milliseconds. Example:1735689600000
This difference causes bugs when mixing systems. Always check whether your system uses seconds or milliseconds!
Common Use Cases
API Responses
Many REST APIs return timestamps as Unix integers. Converting them to readable dates is essential for debugging and displaying to users.
Database Storage
Storing dates as integers is more efficient than strings and allows for easy range queries. You can quickly find all records created in the last 24 hours by comparing timestamps.
Cache Expiration
Setting cache expiration times often requires calculating "current time + X seconds". Unix timestamps make this arithmetic straightforward.
Log Analysis
Log files often contain Unix timestamps. Converting them helps correlate events across different systems and timezones.
Timezone Considerations
Unix timestamps are always in UTC (Coordinated Universal Time). This is crucial because:
- UTC is timezone-independent, making it perfect for distributed systems
- When converting to local time, you must account for timezone offsets
- Daylight Saving Time (DST) changes don't affect Unix timestamps
- Two events happening at the same moment anywhere in the world have the same timestamp
Always store timestamps in UTC and convert to local time only when displaying to users.
Seconds, Milliseconds, Microseconds: Telling Them Apart
Nothing in a bare number says which unit it uses, but the digit count is a reliable clue for dates near the present day.
| Digits | Unit | Example (2025-01-01 UTC) |
|---|---|---|
| 10 | Seconds | 1735689600 |
| 13 | Milliseconds | 1735689600000 |
| 16 | Microseconds | 1735689600000000 |
| 19 | Nanoseconds | 1735689600000000000 |
The most common bug is passing seconds to something that expects milliseconds. JavaScript's Date constructor takes milliseconds, so feeding it a seconds value lands you in January 1970:
new Date(1735689600 * 1000).toISOString()
// "2025-01-01T00:00:00.000Z"
new Date(1735689600).toISOString()
// "1970-01-21T02:08:09.600Z" <- seconds treated as millisecondsIf you must accept both from untrusted input, a magnitude check works for modern dates. Ten-digit values stay below 1e11 until the year 5138, while millisecond values for any date after March 1973 are above it:
function toMs(ts) {
return Math.abs(ts) < 1e11 ? ts * 1000 : ts;
}
toMs(1735689600); // 1735689600000
toMs(1735689600000); // 1735689600000This is a heuristic, not a guarantee. Prefer fixing the contract (document the unit in the field name, such as created_at_ms) over guessing. Also be careful with nanosecond values: they exceed Number.MAX_SAFE_INTEGER (9007199254740991) and lose precision as plain JavaScript numbers, so parse them as BigInt or keep them as strings.
Converting Between Timestamps and Dates
Going from a timestamp to a readable date is one call. Prefer toISOString() for logs and APIs, since it is always UTC and sortable as text. The format follows ISO 8601, and RFC 3339 defines a stricter profile of it for internet protocols.
const ts = 1735689600;
new Date(ts * 1000).toISOString();
// "2025-01-01T00:00:00.000Z"
// Same instant, displayed in two time zones
const d = new Date(ts * 1000);
d.toLocaleString("en-US", { timeZone: "Asia/Kolkata" });
// "1/1/2025, 5:30:00 AM"
d.toLocaleString("en-US", { timeZone: "America/New_York" });
// "12/31/2024, 7:00:00 PM"
// Back to a timestamp
Math.floor(new Date("2025-01-01T00:00:00Z").getTime() / 1000);
// 1735689600In Python, pass an explicit timezone, otherwise fromtimestamp returns a naive local time that is easy to misread:
from datetime import datetime, timezone
datetime.fromtimestamp(1735689600, tz=timezone.utc).isoformat()
# '2025-01-01T00:00:00+00:00'On Linux and macOS you can check a value from the shell too. On macOS, date -u -r 1735689600 prints the date, and on GNU/Linux the equivalent is date -u -d @1735689600.
Pitfall: Date Strings Without a Time Zone
Parsing a string is where timestamp bugs sneak in, because JavaScript treats two similar-looking strings differently. A date-only ISO string (2025-03-09) is parsed as UTC, but a date-time string with no offset (2025-03-09T00:00:00) is parsed as local time. Run on a machine set to New York time, the same "midnight" produces two different timestamps:
// TZ=America/New_York
new Date("2025-03-09").toISOString();
// "2025-03-09T00:00:00.000Z"
new Date("2025-03-09T00:00:00").toISOString();
// "2025-03-09T05:00:00.000Z"The fix is to always include an offset or a Z when you store or transmit a date-time. The same code then gives the same instant on a laptop, a CI runner and a server in another region.
Leap Seconds, Negative Values and Range Limits
- Leap seconds are ignored. POSIX defines every day as exactly 86,400 seconds, so Unix time does not count leap seconds. Systems handle them by repeating or smearing a second, which is why subtracting two timestamps across a leap second can be off by one in real elapsed time.
- Negative values are valid. Timestamps before 1970 are negative.
-2147483648is 1901-12-13T20:45:52Z, which is the wrap-around date from the 2038 problem. - JavaScript has its own limit. A
Dateholds at most 8.64e15 milliseconds either side of the epoch, which is the year 275760. One more millisecond gives an invalid date andgetTime()returnsNaN. - Timestamps measure instants, not calendar dates. A birthday or a billing day is a calendar concept. Store it as a date plus a time zone rule, not as midnight UTC, or users in other zones will see the wrong day.
Seeing the 2038 Overflow Yourself
The largest signed 32-bit value is 2147483647. Converting it shows the exact last second, and forcing the next value into a 32-bit slot shows the wrap:
new Date(2147483647 * 1000).toISOString();
// "2038-01-19T03:14:07.000Z"
new Int32Array([2147483648])[0];
// -2147483648
new Date(-2147483648 * 1000).toISOString();
// "1901-12-13T20:45:52.000Z"The risk today is less about laptops and more about embedded devices, old file formats and database columns declared as 32-bit integers. If you design a schema now, use a 64-bit integer column or a native timestamp type, and check any third-party format that stores seconds in four bytes.
Timestamps in Tokens and Expiry Logic
JSON Web Tokens use Unix seconds (not milliseconds) for the registered claims exp, nbf and iat, as defined in RFC 7519. A classic bug is comparing exp against Date.now() directly, which is 1000 times larger, so every token looks expired. Compare in the same unit:
const nowSec = Math.floor(Date.now() / 1000);
const expiresAt = nowSec + 3600; // one hour from now
const isExpired = (exp) => exp <= nowSec;To inspect a token's claims, paste it into the JWT Decoder, then paste the exp value into the Timestamp Converter to read it as a date. The JWT decoder guide explains how the claims fit together. Allow a small clock-skew tolerance when validating, since servers rarely agree to the second.
Frequently Asked Questions
How do I convert a Unix timestamp to a date?
Multiply seconds by 1000 and pass the result to new Date() in JavaScript, or use datetime.fromtimestamp(ts, tz=timezone.utc) in Python. Paste the value into the Timestamp Converter if you just need a quick answer.
Is a Unix timestamp in seconds or milliseconds?
The traditional definition is seconds, and about ten digits today. JavaScript, Java's System.currentTimeMillis() and many web APIs use milliseconds, which is about thirteen digits. Check the API documentation, because the number itself carries no unit.
Do Unix timestamps change with time zones?
No. A timestamp identifies one instant, so it is identical everywhere. Only the human-readable rendering changes with the time zone, which is why you should convert at the display layer.
What happens in 2038?
Software that stores seconds in a signed 32-bit integer overflows after 2038-01-19T03:14:07Z and wraps to a date in 1901. Systems that use 64-bit time values are unaffected, so the work is auditing old code, firmware and schemas.
Why does my timestamp show 1970?
You most likely passed seconds where milliseconds were expected, or the value was zero or missing. A date in January 1970 is the usual symptom, as shown in the example above.
Converting Human Dates
Calculating these large numbers in your head is impossible. Whether you're debugging an API response or setting a database expiration, you need a quick way to convert between human-readable dates and Unix seconds.
Our Timestamp Converter treats a 10-digit number as seconds and any other length as milliseconds, and also accepts date strings such as ISO 8601. It's perfect for:
- Converting API response timestamps to readable dates
- Reading an expiry or created-at value from a log, cache header or token
- Debugging time-related bugs in your applications
- Comparing the UTC and local-time rendering of the same instant
Written by Kashyap Thakar
Spotted a mistake? Let me know