Dates and times · 1 / 15 Theory

Dates and times: date, time and datetime

Almost all real applications work with dates: orders, bookings, birthdays, logs. They look simple, but they have traps (months of different lengths, leap years, time zones). Python includes the datetime module, which solves it all with three main types.

TypeRepresentsExample
datejust a date15 March 2026
timejust a time09:30:00
datetimedate and time together15 March 2026, 09:30
timedeltaa duration2 days and 3 hours

Creating a date

from datetime import date

f = date(2026, 3, 15)
print(f)                         # 2026-03-15
print(f.year, f.month, f.day)    # 2026 3 15
print(f.weekday())               # 6   (0 = Monday ... 6 = Sunday)
print(f.isoformat())             # 2026-03-15

Impossible dates raise a ValueError: the module knows how many days each month has.

from datetime import date

try:
    date(2026, 2, 30)
except ValueError as e:
    print(e)   # day is out of range for month

print(date(2024, 2, 29))   # 2024-02-29  (2024 is a leap year)

Today's date and time

date.today() and datetime.now() return the current moment. Their value changes on every run, so in programs that must always give the same result (like the exercises in this lesson) the dates are read from the input instead of asking for the current one.

from datetime import date, datetime

today = date.today()        # for example: 2026-11-14
now = datetime.now()        # for example: 2026-11-14 10:25:31.123456
print(type(today).__name__) # date
print(type(now).__name__)   # datetime

Date and time with datetime

from datetime import datetime

moment = datetime(2026, 3, 15, 9, 30)
print(moment)                          # 2026-03-15 09:30:00
print(moment.hour, moment.minute)      # 9 30
print(moment.date())                   # 2026-03-15
print(moment.time())                   # 09:30:00

Comparing dates

Dates are compared with the normal operators (<, ==, >=...), and can be sorted and used with min and max.

from datetime import date

dates = [date(2026, 3, 15), date(2025, 12, 1), date(2026, 1, 20)]
print(sorted(dates))   # [datetime.date(2025, 12, 1), datetime.date(2026, 1, 20), datetime.date(2026, 3, 15)]
print(min(dates))      # 2025-12-01
print(date(2026, 1, 1) < date(2026, 1, 2))   # True

From text to date: fromisoformat

The ISO 8601 standard format (YYYY-MM-DD, and YYYY-MM-DD HH:MM for date and time) is the most convenient and the one that sorts well as text. fromisoformat turns it into an object.

from datetime import date, datetime

print(date.fromisoformat("2026-03-15"))                  # 2026-03-15
print(datetime.fromisoformat("2026-03-15 09:30"))         # 2026-03-15 09:30:00
print(date.fromisoformat("2026-03-15").weekday())          # 6

Check with Day of the week how the days are named from the number returned by weekday().

Date objects are immutable

Once created, a date does not change: operating on it (for example, adding days) returns a new date. This avoids many mistakes, just like with strings and tuples.

from datetime import date

f = date(2026, 3, 15)
g = f.replace(day=1)   # a NEW date with the day changed
print(f, g)            # 2026-03-15 2026-03-01