Unix Timestamp Explained: Seconds vs Milliseconds, Conversion and 2038
A Unix timestamp is the number of seconds since 1 January 1970 UTC. It is simple, compact and time-zone free, yet unit mix-ups and old 32-bit storage still cause real bugs. Learn how it works, how to convert it in Python, JavaScript and SQL, and what the 2038 problem means, with tested code.
- A Unix timestamp counts seconds since 1970-01-01 00:00:00 UTC. It is the same number everywhere in the world at a given moment.
- 10 digits usually means seconds and 13 digits means milliseconds. JavaScript uses milliseconds; Python and most databases use seconds.
- Always convert with an explicit UTC zone. Local conversions differ between machines.
- Signed 32-bit storage overflows at 2038-01-19 03:14:07 UTC; the next second wraps to 1901 in our test.
Logs, APIs, JWT tokens, databases and file systems all record time as a single integer like 1794801600. That is a Unix timestamp, also called epoch time or POSIX time. It is the lingua franca of time in computing, because a plain number is easy to store, sort, compare and send. The trouble starts when one system means seconds and another means milliseconds, or when someone converts it in the wrong time zone.
We tested the conversions below with Python 3.13.6, Node.js 24.22.1 and SQLite 3.50.4. A few results were specific to Windows, and we point those out.
What is a Unix timestamp?
It is the number of seconds that have passed since the Unix epoch: midnight UTC on 1 January 1970. The epoch itself is timestamp 0. One day later is 86,400. Times before 1970 are negative. Because it counts from a fixed moment in UTC, a timestamp has no time zone: at any given instant, every computer on Earth agrees on the same number. Time zones only appear when you turn it into a human-readable date.
Why timestamps are so popular
- Compact: one integer instead of a string with date, time and offset.
- Unambiguous: no day-first or month-first confusion, no time zone guessing.
- Easy maths: subtract two timestamps to get elapsed seconds; sorting numbers sorts by time.
- Universal: every language and database can produce and read one.
A worked example
from datetime import datetime, timezone
print(datetime.fromtimestamp(1794801600, tz=timezone.utc))
# 2026-11-16 04:00:00+00:00
print(datetime(2026, 11, 17, tzinfo=timezone.utc).timestamp())
# 1794873600.0
The difference between those two values is 72,000 seconds, which is 20 hours: from 04:00 on the 16th to midnight on the 17th. Every conversion to a calendar date needs a time zone; here we always used UTC.
A timestamp is the same in every zone. 05:30 on 17 November in India (UTC+5:30) is the same instant as midnight UTC, and Python returned the same number for both:
ist = timezone(timedelta(hours=5, minutes=30))
int(datetime(2026, 11, 17, 5, 30, tzinfo=ist).timestamp())
# 1794873600
Seconds vs milliseconds (and smaller)
This is the number one source of timestamp bugs. Different systems use different units:
| Unit | Digits today | Example | Used by |
|---|---|---|---|
| Seconds | 10 | 1794801600 | Unix tools, Python time.time(), most SQL, JWT exp |
| Milliseconds | 13 | 1794801600000 | JavaScript Date.now(), Java, many JSON APIs |
| Microseconds | 16 | 1794801600000000 | PostgreSQL internals, some logs |
| Nanoseconds | 19 | 1794801600000000000 | Go, Python time.time_ns() |
Our small helper guessed the unit from the digit count and labelled the four example values ['s', 'ms', 'us', 'ns']. The digit rule works for dates between 2001 and 2286, which covers practically all real data. Still, the reliable fix is to name the unit in your API fields, such as created_at_ms.
What happens when you get it wrong? Passing milliseconds to a function expecting seconds produces a date tens of thousands of years in the future. On our Windows machine, Python refused it outright:
datetime.fromtimestamp(1794801600000, tz=timezone.utc)
# OSError: [Errno 22] Invalid argument
datetime.fromtimestamp(1794801600000 / 1000, tz=timezone.utc)
# 2026-11-16 04:00:00+00:00
On Linux the same mistake typically raises a ValueError saying the year is out of range instead; we only tested Windows here, so expect the error type to vary by platform. The opposite mistake is quieter and worse. Passing seconds where JavaScript expects milliseconds gave us a plausible-looking date in 1970:
new Date(1794801600).toISOString() // 1970-01-21T18:33:21.600Z
new Date(1794801600 * 1000).toISOString() // 2026-11-16T04:00:00.000Z
If you see dates in January 1970 in your app, a seconds value went into a milliseconds field.
Unix timestamps in Python
import time
from datetime import datetime, timezone
time.time() # float seconds, e.g. 1794801600.123
int(time.time()) # whole seconds; 10 digits today
time.time_ns() # integer nanoseconds
dt = datetime.fromtimestamp(1794801600, tz=timezone.utc) # timestamp to datetime
ts = int(dt.timestamp()) # datetime to timestamp
Always pass tz=timezone.utc to fromtimestamp. Without it you get naive local time, which differs between your laptop, a server and a container. The older utcfromtimestamp() is deprecated; our Python 3.13 run printed a DeprecationWarning for it. Our Python datetime guide covers aware and naive values in depth.
Unix timestamps in JavaScript
Date.now() // milliseconds, 13 digits
Math.floor(Date.now() / 1000) // seconds
new Date(1794801600 * 1000).toISOString() // seconds to Date
Math.floor(Date.parse("2026-11-17T00:00:00Z") / 1000)
// 1794873600
Everything in JavaScript’s Date is milliseconds, so multiply by 1,000 on the way in and divide on the way out. Round down with Math.floor rather than Math.round, so you never produce a timestamp in the future. Our JavaScript Date guide covers parsing and time zones.
Unix timestamps in SQL
SELECT unixepoch('2026-11-16 04:00:00'), -- 1794801600
datetime(1794801600, 'unixepoch'), -- 2026-11-16 04:00:00
strftime('%s', '2026-11-16 04:00:00'); -- '1794801600'
Those are SQLite functions; all three ran in our test, and note that strftime('%s') returns text. Other databases use different names: UNIX_TIMESTAMP() and FROM_UNIXTIME() in MySQL, EXTRACT(EPOCH FROM ts) and to_timestamp() in PostgreSQL, and DATEDIFF_BIG(SECOND, '1970-01-01', ts) or DATEADD in SQL Server. Our SQL date functions guide lists them side by side.
Dates before 1970
Negative timestamps represent dates before the epoch, but support varies. On Windows, Python refused to convert even one day before 1970:
datetime.fromtimestamp(-86400, tz=timezone.utc)
# OSError: [Errno 22] Invalid argument
EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
EPOCH + timedelta(seconds=-86400)
# 1969-12-31 00:00:00+00:00
Adding a timedelta to the epoch works on every platform, because it never calls the operating system’s time functions. Use it whenever historical dates such as birthdays may appear.
Leap seconds: Unix time ignores them
A leap second was added at the end of 2016, so the real gap between 23:59:59 on 31 December 2016 and midnight on 1 January 2017 was two seconds. Unix time pretends every day has exactly 86,400 seconds:
datetime(2017, 1, 1, tzinfo=timezone.utc).timestamp() - \
datetime(2016, 12, 31, 23, 59, 59, tzinfo=timezone.utc).timestamp()
# 1.0
Python reported one second. For almost all applications this simplification is a feature, because it keeps date maths simple. Only scientific and some financial systems need true elapsed time, and they use other time scales.
The year 2038 problem
Many older systems stored timestamps in a signed 32-bit integer. Its maximum value is 2,147,483,647:
datetime.fromtimestamp(2**31 - 1, tz=timezone.utc)
# 2038-01-19 03:14:07+00:00
struct.pack("<i", 2**31)
# struct.error: 'i' format requires -2147483648 <= number <= 2147483647
wrapped = struct.unpack("<i", struct.pack("<I", 2**31))[0]
EPOCH + timedelta(seconds=wrapped)
# 1901-12-13 20:45:52+00:00
One second after 03:14:07 UTC on 19 January 2038, a signed 32-bit counter wraps to its most negative value, which means 13 December 1901. Modern 64-bit systems are safe for billions of years, but 32-bit values still hide in embedded devices, old file formats, database columns defined as INT, and C structures. Audit any timestamp column or field that is 32 bits wide, especially for dates far in the future such as loan end dates or certificate expiry.
Common timestamp bugs
- Unit mix-ups between seconds and milliseconds.
- Local conversion without an explicit zone, giving different dates on different machines.
- Floats losing precision: a float cannot exactly hold nanosecond timestamps; use integers.
- Storing as text, which then sorts wrongly if lengths differ.
- 32-bit columns that will overflow in 2038.
- Comparing expiry times with the wrong unit, so tokens expire at once or never. JWT
expis in seconds, as our JWT guide notes.
Timestamp or datetime column?
For databases, a native timestamp type with time zone support is usually better than a raw integer: it is readable in queries, validated by the database, and works with date functions. Integer timestamps make sense for high-volume event data, cross-system interchange and caches. Whichever you choose, store UTC, document the unit, and convert to local time only in the user interface.
Tip: when debugging, paste a suspicious number into a converter or run datetime.fromtimestamp(x, tz=timezone.utc). A date in 1970 or far in the future almost always means a unit mix-up.
Testing tips
When code handles timestamps, add test cases for the epoch (0), a negative value, a millisecond value passed where seconds are expected, the 2038 boundary (2,147,483,647 and one second later), and a value near midnight UTC that falls on different dates in different zones. Freeze or inject the current time so tests do not depend on when they run, as shown in our Python mocking guide. These few cases catch nearly every timestamp bug we have described.
Final thoughts
A Unix timestamp is the simplest way to record a moment: one integer, the same everywhere. Its pitfalls are just as simple once you know them. Check the unit by its digits, convert with an explicit UTC zone, use timedelta for dates before 1970 on Windows, and make sure nothing stores it in 32 bits. Do that, and timestamps become the most reliable part of your date handling.
FAQ
What is a Unix timestamp?
It is the number of seconds since 1 January 1970 at 00:00 UTC, known as the Unix epoch. It is the same number everywhere in the world at a given moment.
How do I know if a timestamp is in seconds or milliseconds?
Count the digits. Current dates have 10 digits in seconds and 13 in milliseconds. Better still, name the unit in the field or document it in the API.
How do I convert a Unix timestamp to a date in Python?
Call datetime.fromtimestamp with the timestamp and tz set to timezone.utc. Divide by 1000 first if the value is in milliseconds.
What is the year 2038 problem?
Signed 32-bit integers can hold timestamps only up to 19 January 2038 at 03:14:07 UTC. One second later they overflow and wrap to a date in 1901.
Does a Unix timestamp have a time zone?
No. It always counts from midnight UTC on 1 January 1970. Time zones only matter when you convert it into a readable local date and time.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.