Convert a timestamp to a date
Use datetime.fromtimestamp() with an explicit time zone. Without the tz argument, Python creates a local date without time zone information.
from datetime import datetime, timezone
ts = 1711972800
date = datetime.fromtimestamp(ts, tz=timezone.utc)
print(date.isoformat())
# 2024-04-01T12:00:00+00:00Get Unix seconds from a date
date = datetime(2024, 4, 1, 12, 0, 0, tzinfo=timezone.utc)
print(date.timestamp()) # 1711972800.0Convert milliseconds without floating-point arithmetic
Dividing by 1,000 is sufficient for many uses. To preserve an exact microsecond fraction, separate whole seconds from the remainder.
from datetime import timedelta
ms = 1711972800123
seconds, remainder = divmod(ms, 1000)
date = datetime.fromtimestamp(seconds, timezone.utc)
date += timedelta(milliseconds=remainder)
print(date.isoformat())
# 2024-04-01T12:00:00.123000+00:00Display the time in Paris
from zoneinfo import ZoneInfo
print(date.astimezone(ZoneInfo('Europe/Paris')).isoformat())
# 2024-04-01T14:00:00.123000+02:00zoneinfo is available from Python 3.9. Time zone data must be installed on the system or supplied by the tzdata.
To measure execution time, use time.perf_counter(). The wall clock may be adjusted while your program runs.
References
Python — datetime · zoneinfo · perf_counter
English version published September 26, 2026.