You’ll be able to
- Create and format dates with
datetime - Parse strings into date objects with
strptime - Add and subtract time with
timedelta - Handle timezones properly with
zoneinfo
Loading lesson…
Why this matters
Naive datetime objects (no tzinfo) are the root cause of daylight-saving bugs and off-by-one-day reports in every analytics stack. Since Python 3.9, zoneinfo replaces pytz; datetime.now(tz=...) should always be timezone-aware in production.
Common pitfalls
- Comparing naive and aware datetimes — raises
TypeError; standardize on UTC-aware at boundaries. - Using
datetime.utcnow()— returns naive; preferdatetime.now(timezone.utc)for explicit UTC. - Doing calendar math with
timedelta(days=30)for "one month" — usedateutil.relativedeltafor real months.