How to Convert a Unix Timestamp in Milliseconds to a datetime in Python
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
from datetime import UTC, datetime
datetime.fromtimestamp(<milliseconds> / 1000, tz=UTC)
| Part | What it means |
|---|---|
<milliseconds> | The timestamp: milliseconds since 1 January 1970, 00:00 UTC |
/ 1000 | Turns milliseconds into the seconds fromtimestamp() expects. The .5 that's left keeps the milliseconds |
tz=UTC | Returns 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 datetime import UTC, datetime
milliseconds = 1767225600500
moment = datetime.fromtimestamp(milliseconds / 1000, tz=UTC)
print(moment)
print(moment.isoformat())

milliseconds / 1000is1767225600.5: whole seconds plus half a second.tz=UTCmakes 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
uv run python from_milliseconds.py
2026-01-01 00:00:00.500000+00:00
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
| Mistake | What happens | Do 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 now | Divide by 1000 |
Leaving out tz=UTC | You 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.500000 | Pass 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 offset | Use datetime.fromtimestamp(timestamp, tz=UTC), as the warning itself suggests |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
ValueError: year must be in 1..9999, not ... | The timestamp is in milliseconds (13 digits) and wasn't divided | Use / 1000 |
| The time is off by a few hours | No tz, so Python used your computer's time zone | Add tz=UTC |
| The date is in January 1970 | A seconds timestamp was divided by 1000 | Count 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
- datetime.fromtimestamp() — the method and its
tzargument. - datetime.UTC — the UTC time zone.
- datetime.utcfromtimestamp() — the older method, and why
fromtimestamp()is preferred.
