date.year / month / day

Plain ints, already validated. They are attributes, not methods — no parentheses — and they are read-only: build a changed copy with replace().

date attributesPython 2.3+Live demo
Common call
d.year, d.month, d.day
Returns
(2026, 9, 29)
Replaces
int(text[:4]) slicing of 'YYYY-MM-DD' strings
Watch out
d.year = 2027 raises AttributeError — use d.replace(year=2027)
d.year · d.month · d.day
→ int

Demo

Live evaluation
Parse a date or a datetime and read its date fields. A datetime has them too (it is a date subclass).
Try:
Inputs
whenstran ISO 8601 date or timestamp
Code
from datetime import datetime
d = datetime.fromisoformat('2026-09-29')
(d.year, d.month, d.day, f'{d.day}.{d.month}.{d.year}')
Result
(2026, 9, 29, '29.9.2026')

The fields are the local wall-clock date as written: 2026-01-05T23:59:59+05:00 reports day 5 even though it is already 18:59 UTC — use astimezone(UTC) first if you want the UTC date. The f-string shows the ints without zero padding.

Attributes

AttributeTypeMeaning
yearintMINYEAR (1) to MAXYEAR (9999).
monthint1 to 12 — not zero-based.
dayint1 to the number of days in the month.

Common patterns

Group by year and month
A (year, month) tuple is a natural dict key.
from collections import defaultdict
by_month = defaultdict(list)
for d in dates:
    by_month[(d.year, d.month)].append(d)
First day of the month
replace, not assignment.
first = d.replace(day=1)
Quarter from month
Integer arithmetic on the month field.
quarter = (d.month - 1) // 3 + 1

Examples

1. Read the fields
from datetime import date d = date(2026, 9, 29) (d.year, d.month, d.day)
Returns
(2026, 9, 29)
2. Inherited by datetime
from datetime import datetime datetime(2026, 9, 29, 14, 30).year
Returns
2026
3. Without zero padding
from datetime import date d = date(2026, 9, 5) f'{d.day}/{d.month}/{d.year}'
Returns
'5/9/2026'
4. Quarter of the year
from datetime import date (date(2026, 9, 29).month - 1) // 3 + 1
Returns
3
5. Read-only
from datetime import date d = date(2026, 9, 29) d.year = 2027
Returns
AttributeError: attribute 'year' of 'datetime.date' objects is not writable

Pitfalls

1. Assigning to a field
Dates are immutable (they are hashable dict keys). replace() returns a new date.
d.month = 10
from datetime import date
d = date(2026, 9, 29)
d.month = 10
AttributeError: attribute 'month' of 'datetime.date' objects is not writable
replace(month=10)
from datetime import date
date(2026, 9, 29).replace(month=10)
datetime.date(2026, 10, 29)
2. Calling them like methods
year, month and day are ints. (weekday() IS a method — the inconsistency trips people up.)
d.year()
from datetime import date
date(2026, 9, 29).year()
TypeError: 'int' object is not callable
d.year
from datetime import date
date(2026, 9, 29).year
2026

When to use

Use it
  • Grouping, bucketing and comparing by calendar parts
  • Building custom labels
Reach for something else
  • Formatting whole dates → strftime or isoformat
  • Changing a field → replace()

Notes

CPython impl
Read-only getset descriptors on the C type; the values are unpacked from the 4-byte date data
Also on
datetime (inherited); time has hour/minute/second instead
Validity
Always consistent — the constructor already rejected 31 April or month 13

FAQ

d.year — an int attribute, no parentheses. The same works on a datetime. For the current year: date.today().year.