Store and compare a date without a time-of-day component.
How It Works
datetime.date(year, month, day) validates Gregorian calendar components and offers comparisons and calendar calculations. A date is not a timestamp and contains no timezone information.
Step-by-Step Example
from datetime import date
start_date = date(2026, 10, 17)
end_date = date(2026, 10, 21)
print(start_date.isoformat())
print((end_date - start_date).days)
Understanding the Example
- The expression 'date(2026, 10, 17)' supplies the value stored as 'start_date'. This value is then used or checked by the later statements.
- The expression 'date(2026, 10, 21)' supplies the value stored as 'end_date'. This value is then used or checked by the later statements.
A successful run of this example prints the following output:
2026-10-17
4
Where This Fits in a Real Program
For event timestamps, distinguish the displayed local clock from the instant that it represents. Store or exchange an unambiguous aware value, then convert it when displaying it to a user. For repeated calendar appointments, use a geographic timezone so daylight-saving rules are not lost.
Practice: Try It Yourself
Use datetime when the hour, minute, or instant must be stored.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Constructing an invalid date, such as February 30, or treating a date as a timezone-aware instant.
- Calculate how many days remain between two chosen calendar dates.
Key Takeaways
- Store and compare a date without a time-of-day component.
- datetime.date(year, month, day) validates Gregorian calendar components and offers comparisons and calendar calculations.
- Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.