Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

datetime.date

Represents a calendar date without a time of day or time zone.

Version: Python 3.14; repository image, production runtime not attested

Syntax

datetime.date(year, month, day)

Represents a calendar date without a time of day or time zone.

Date arithmetic follows the actual calendar, including leap days. Impossible dates raise ValueError when constructed.

Store a civil date without time or zone in the proleptic Gregorian calendar. Supply integer year 1–9999, month 1–12 and a day valid for that month and year. Subtracting two dates yields a timedelta. Adding a day to February 28 in 2024 reaches February 29, but February 29 in 2023 raises ValueError.

from datetime import date, timedelta
assert date(2000, 2, 28) + timedelta(days=1) == date(2000, 2, 29)
assert date(1900, 2, 28) + timedelta(days=1) == date(1900, 3, 1)
assert date(2024, 3, 1) - date(2024, 2, 28) == timedelta(days=2)

Examples

import datetime
d = datetime.date(2024, 2, 28)
print(d + datetime.timedelta(days=1))
try:
    datetime.date(2023, 2, 29)
except ValueError as error:
    print(type(error).__name__)

Result

2024-02-29
ValueError

Store a civil date without time or zone in the proleptic Gregorian calendar. Supply integer year 1–9999, month 1–12 and a day valid for that month and year. Subtracting two dates yields a timedelta. Adding a day to February 28 in 2024 reaches February 29, but February 29 in 2023 raises ValueError.

Sources