SyntaxStudy
Sign Up
Python Python datetime Summary
Python Beginner 3 min read

Python datetime Summary

datetime Summary

Python datetime provides date arithmetic, formatting, timezone handling, and comparison. Use zoneinfo for IANA zones, dateutil for flexible parsing, pandas for bulk operations, and ISO 8601 for serialisation.

Example
from datetime import datetime, timezone, timedelta
from zoneinfo import ZoneInfo

now_utc = datetime.now(tz=timezone.utc)
tomorrow = now_utc + timedelta(days=1)
ny_time  = now_utc.astimezone(ZoneInfo("America/New_York"))
iso_str  = now_utc.isoformat()
back     = datetime.fromisoformat(iso_str)
Pro Tip

Store UTC, display local. Parse at boundaries, use timedelta inside. Serialise as ISO 8601.