typestar

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

  1. Adding a timedelta shifts a datetime.
  2. isoformat and fromisoformat round-trip text.
  3. strftime formats; 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.

Type this snippet

Step 4 of 5 in Data formats, step 20 of 41 in Domain tools.

← Previous Next →

Dates and times in other languages