Timestamps

EasyDatetime Programs · 14 of 20

Write a Python program to convert between a datetime and a Unix timestamp.

Advertisement
Output
1767225600.0
2026-01-01 00:00:00+00:00

Approach

  1. Use UTC to get the same result on every computer.

Solution

Python
from datetime import datetime, timezone dt = datetime(2026, 1, 1, tzinfo=timezone.utc)ts = dt.timestamp()print(ts)print(datetime.fromtimestamp(ts, tz=timezone.utc))
Output
1767225600.0
2026-01-01 00:00:00+00:00

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Leave a Comment