Dates and times in Python
Arithmetic and formatting with datetime and timedelta.
from datetime import datetime, timedelta, timezone
launch = datetime(2026, 7, 29, 9, 30, tzinfo=timezone.utc)
deadline = launch + timedelta(days=14)
countdown = deadline - launch
stamp = launch.isoformat()
parsed = datetime.fromisoformat("2026-07-29T09:30:00+00:00")
label = launch.strftime("%Y-%m-%d %H:%M")
is_before = launch < deadline
days_left = countdown.days
How it works
- Adding a
timedeltashifts a datetime. isoformatandfromisoformatround-trip text.strftimeformats; subtraction gives a duration.
The run, in numbers
- Lines
- 12
- Characters to type
- 368
- Tokens
- 88
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 50 seconds.
Step 4 of 5 in Data formats, step 20 of 41 in Domain tools.