Attributes of a date in Python’s datetime module are the building blocks for working with date objects — a date instance holds year, month, and day values, plus convenient methods for weekday calculation, formatting, and string representation. These attributes and methods are fast, reliable, and timezone-naive (no hour/minute/second or tzinfo). In 2026, understanding date attributes remains core for data analysis, logging, scheduling, reporting, and any code dealing with calendar dates — whether extracting components, computing days of week, or formatting for display/export.
Here’s a complete, practical guide to the attributes and methods of a date object: core properties, weekday calculations, formatting options, real-world patterns, and modern best practices with type hints and error handling.
A date object has three read-only integer attributes: year, month, and day — always valid (1–12 for month, 1–31 for day depending on month/year).
from datetime import date
d = date(2026, 2, 10)
print(d.year) # 2026
print(d.month) # 2
print(d.day) # 10
print(d) # 2026-02-10 (default __str__ format)
Weekday methods give day-of-week info — weekday() returns 0 (Monday) to 6 (Sunday); isoweekday() returns 1 (Monday) to 7 (Sunday) — useful for scheduling, reporting, or filtering.
print(d.weekday()) # 1 (Tuesday, since Feb 10, 2026 is a Tuesday)
print(d.isoweekday()) # 2 (ISO standard: Monday=1)
# Check if weekend
if d.weekday() >= 5:
print("Weekend!")
Formatting methods convert date to strings — isoformat() gives standard YYYY-MM-DD; strftime() supports custom formats; ctime() gives a human-readable ctime-style string.
print(d.isoformat()) # '2026-02-10'
print(d.strftime("%A, %B %d, %Y")) # 'Tuesday, February 10, 2026'
print(d.strftime("%Y/%m/%d")) # '2026/02/10'
print(d.strftime("%b %d, %Y")) # 'Feb 10, 2026'
print(d.ctime()) # 'Tue Feb 10 00:00:00 2026' (always midnight)
Real-world pattern: processing date columns in data analysis — extract attributes or format dates for reports, filtering, or grouping.
import pandas as pd
df = pd.DataFrame({
'event_date': [date(2026, 1, 15), date(2026, 2, 10), date(2026, 3, 5)]
})
# Extract attributes as new columns
df['year'] = df['event_date'].apply(lambda d: d.year)
df['month'] = df['event_date'].apply(lambda d: d.month)
df['weekday'] = df['event_date'].apply(lambda d: d.weekday()) # 0=Monday
# Format for display
df['formatted'] = df['event_date'].apply(lambda d: d.strftime("%A, %b %d, %Y"))
print(df)
# event_date year month weekday formatted
# 0 2026-01-15 2026 1 2 Thursday, Jan 15, 2026
# 1 2026-02-10 2026 2 1 Tuesday, Feb 10, 2026
# 2 2026-03-05 2026 3 3 Thursday, Mar 05, 2026
Best practices make date attribute usage safe, readable, and performant. Use date.today() for current date — always timezone-naive unless you add tzinfo. Prefer isoweekday() for ISO-compliant weekday numbering (1=Monday). Use strftime() with standard codes — ISO 8601 (%Y-%m-%d) for interoperability. Modern tip: use Polars for large date columns — pl.col("date").dt.year(), .dt.weekday(), .dt.strftime("%Y-%m-%d") are 10–100× faster than pandas apply. Add type hints — date or datetime.date — improves readability and mypy checks. Handle invalid dates — wrap date(year, month, day) in try/except ValueError. Combine with timedelta — date.today() + timedelta(days=7) for date arithmetic. Avoid naive datetimes when timezone matters — use datetime with ZoneInfo for awareness.
The attributes of a date object — year, month, day, weekday methods, and formatting — give you full control over calendar dates. In 2026, extract components vectorized, format consistently, use type hints, and prefer Polars for scale. Master date attributes, and you’ll handle reporting, filtering, scheduling, and analytics with precision and efficiency.
Next time you have a date object — access its attributes. It’s Python’s cleanest way to say: “Give me the year, month, day, or weekday — instantly.”