Reference
Common datetime patterns in Python
Parsing, formatting, time zones, and durations — the datetime snippets you keep looking up.
May 2026 · 7 min read · 2 views · 0 hearts
Now, and timezone-aware now
from datetime import datetime, timezone, timedelta
naive = datetime.now()
aware = datetime.now(timezone.utc)Format and parse
aware.strftime("%Y-%m-%d %H:%M") # to string
datetime.strptime("2026-03-01", "%Y-%m-%d") # from stringDurations with timedelta
deadline = aware + timedelta(days=7)
remaining = deadline - awarePrefer timezone-aware datetimes for anything stored or compared across systems. Use the standard library's zoneinfo for named zones like Europe/Paris.
Sponsored
Sponsored
Reserved space — layout preview until AdSense is connected
Comments
Questions, corrections, and tips stay visible for everyone reading this page.
Join the discussion
No comments yet
Be the first to leave a note — it helps the next reader.