Skip to main content

How to Convert a Unix Timestamp in Milliseconds to a datetime in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Many APIs and JavaScript programs give times as a Unix timestamp in milliseconds: a 13-digit number such as 1767225600500. Python's datetime.fromtimestamp() counts in seconds, so you divide by 1000 first and pass a time zone. This guide does it on Python 3.15.2.

Syntax​

Syntax
from datetime import UTC, datetime

datetime.fromtimestamp(<milliseconds> / 1000, tz=UTC)
PartWhat it means
<milliseconds>The timestamp: milliseconds since 1 January 1970, 00:00 UTC
/ 1000Turns milliseconds into the seconds fromtimestamp() expects. The .5 that's left keeps the milliseconds
tz=UTCReturns the time in UTC, with its offset attached. UTC is datetime.timezone.utc under a shorter name

The datetime documentation says fromtimestamp() returns the date and time "corresponding to the POSIX timestamp", and that without tz, "the timestamp is converted to the platform's local date and time, and the returned datetime object is naive", which means it carries no time zone.

The Example​

The script converts the timestamp 1767225600500, which is half a second after midnight UTC on 1 January 2026, and prints it twice: as Python shows a datetime, and in ISO 8601 form.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

from_milliseconds.py
from datetime import UTC, datetime

milliseconds = 1767225600500
moment = datetime.fromtimestamp(milliseconds / 1000, tz=UTC)

print(moment)
print(moment.isoformat())

The from_milliseconds.py script in a macOS-style code window: it divides the timestamp 1767225600500 by 1000, converts it with datetime.fromtimestamp and tz=UTC, and prints the result and its isoformat.

  • milliseconds / 1000 is 1767225600.5: whole seconds plus half a second.
  • tz=UTC makes the result a UTC time with its offset.
  • isoformat() writes the same moment as ISO 8601 text, the form APIs and files usually use.

Step 2: Run It​

Terminal
uv run python from_milliseconds.py
Output
2026-01-01 00:00:00.500000+00:00
2026-01-01T00:00:00.500000+00:00

The terminal after uv run python from_milliseconds.py: 2026-01-01 00:00:00.500000+00:00, then the ISO form 2026-01-01T00:00:00.500000+00:00.

The 500 milliseconds survived as .500000 (microseconds), and +00:00 shows the time is in UTC. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Passing the milliseconds without dividing: datetime.fromtimestamp(1767225600500, tz=UTC)ValueError: year must be in 1..9999, not 57971: Python read it as seconds, about 56,000 years from nowDivide by 1000
Leaving out tz=UTCYou get the computer's local time with no offset. On a computer set to New York time, the same timestamp prints 2025-12-31 19:00:00.500000Pass tz=UTC, then convert to another zone if you need one
Using datetime.utcfromtimestamp()DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version, and the result has no offsetUse datetime.fromtimestamp(timestamp, tz=UTC), as the warning itself suggests

Troubleshooting​

SymptomLikely causeFix
ValueError: year must be in 1..9999, not ...The timestamp is in milliseconds (13 digits) and wasn't dividedUse / 1000
The time is off by a few hoursNo tz, so Python used your computer's time zoneAdd tz=UTC
The date is in January 1970A seconds timestamp was divided by 1000Count the digits: 10 means seconds, 13 means milliseconds

FAQ​

How do I tell seconds from milliseconds?​

Count the digits. Timestamps from our time have 10 digits in seconds and 13 in milliseconds. datetime.fromtimestamp(1767225600, tz=UTC) (seconds, no division) prints 2026-01-01 00:00:00+00:00.

Why / 1000 and not // 1000?​

/ keeps the fraction, so the milliseconds survive as .500000. // would drop them.

Conclusion​

datetime.fromtimestamp(milliseconds / 1000, tz=UTC) turned 1767225600500 into 2026-01-01 00:00:00.500000+00:00. Divide by 1000, because Python counts in seconds, and pass tz=UTC, so the result doesn't depend on the computer's time zone.

Additional Resources​