Convert Unix Timestamp to Date
Convert a Unix timestamp to a human-readable date.
Code
Python
from datetime import datetime
ts = 1709827200 # Unix timestamp
dt = datetime.fromtimestamp(ts)
print(dt.strftime("%Y-%m-%d %H:%M:%S"))
# Or with timezone
from datetime import timezone
dt_utc = datetime.fromtimestamp(ts, tz=timezone.utc)
print(dt_utc)Line-by-line explanation
- 1.Import datetime.
- 2.datetime.fromtimestamp() converts seconds since epoch.
- 3.strftime() formats the output.
- 4.Use tz=timezone.utc for UTC.
Expected output
2024-03-07 00:00:00