bhylu.com

Advertisements

Dates

datetime represents dates, times, and time differences. Be explicit about whether a value is a calendar date or an instant with time-zone meaning.

Understand the idea

A date plus a timedelta expresses a number of days cleanly. For real instants, prefer aware datetime values with a documented zone policy. Local daylight-saving changes make calendar scheduling different from assuming every day is an interchangeable number of elapsed seconds. Use fixed dates in tests instead of today's changing date.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

from datetime import date, timedelta
starts_on = date(2026, 3, 1)
print(starts_on + timedelta(days=7))
print((date(2026, 3, 10) - starts_on).days)

Read the result

The results are 2026-03-08 and 9.

Try one change

Parse a known ISO date with date.fromisoformat and handle invalid input deliberately.