The date class in Python's datetime module represents a calendar date using year, month and day values. It provides methods to create, access, modify, compare and format date objects. A date object does not store time or timezone information. It follows the Gregorian calendar and is immutable, meaning its values cannot be changed after creation.
Syntax
class datetime.date(year, month, day)
Parameters:
- year: An integer between 1 and 9999.
- month: An integer between 1 and 12.
- day: An integer representing a valid day in the specified month and year.
Example: Creating Date Object
from datetime import date
my_date = date(2024, 2, 29)
print(my_date)
Output
2024-02-29
Explanation:
- date() constructor creates a date object.
- Arguments follow the year, month and day order.
- Python validates the supplied date, including leap years.
Class and Instance Attributes
Date class provides attributes to access date information and determine the supported date range.
Class Attributes
Attribute | Description |
|---|---|
min | Represents the earliest supported date. |
max | Represents the latest supported date. |
resolution | Represents the smallest possible difference between two distinct date objects. |
Example: Accessing Minimum and Maximum Dates
from datetime import date
print("Minimum Date:", date.min)
print("Maximum Date:", date.max)
print("Resolution:", date.resolution)
Output
Minimum Date: 0001-01-01 Maximum Date: 9999-12-31 Resolution: 1 day, 0:00:00
Instance Attributes
Attribute | Description |
|---|---|
year | Returns the year value. |
month | Returns the month value (1–12). |
day | Returns the day of the month. |
Example: Accessing Date Attributes
from datetime import date
my_date = date(2024, 2, 29)
print("Year:", my_date.year)
print("Month:", my_date.month)
print("Day:", my_date.day)
Output
Year: 2024 Month: 2 Day: 29
Date Class Method
It provides several class methods to create date objects from different values and representations.
1. today()
This method returns the current local date as a date object.
from datetime import date
today = date.today()
print("Current Date:", today)
Output
Current Date: 2026-11-03
2. fromtimestamp()
This method creates a date object from a POSIX timestamp.
from datetime import date
timestamp = 0
result = date.fromtimestamp(timestamp)
print(result)
Output
1970-01-01
3. fromordinal()
It creates a date object from a proleptic Gregorian ordinal. An ordinal represents the number of days counted from January 1, year 1, which has ordinal value 1.
from datetime import date
result = date.fromordinal(1)
print(result)
Output
0001-01-01
4. fromisoformat()
This method converts a valid ISO 8601 date string into a date object.
from datetime import date
date_string = "2024-02-29"
result = date.fromisoformat(date_string)
print(result)
Output
2024-02-29
5. fromisocalendar()
This method creates a date object using ISO calendar year, week number and weekday values.
from datetime import date
result = date.fromisocalendar(2024, 9, 4)
print(result)
Output
2024-02-29
6. strptime()
It converts a date string into a date object using a specified format.
from datetime import datetime
date_string = "29-02-2024"
result = datetime.strptime(date_string, "%d-%m-%Y")
print(result)
Output
2024-02-29 00:00:00
Instance Method of Date Class
They operate on an existing date object. They help retrieve date information, modify values and convert dates into different representations.
1. weekday()
This method returns the weekday as an integer. Monday is represented by 0, while Sunday is represented by 6.
from datetime import date
my_date = date(2024, 2, 29)
print(my_date.weekday())
Output
3
2. isoweekday()
method returns the weekday using the ISO numbering system. Monday is represented by 1, while Sunday is represented by 7.
from datetime import date
my_date = date(2024, 2, 29)
print(my_date.isoweekday())
Output
4
3. isocalendar()
It returns the ISO calendar information for a date. It provides three values:
- year: ISO calendar year.
- week: ISO week number.
- weekday: Weekday number from 1 to 7.
from datetime import date
my_date = date(2024, 2, 29)
result = my_date.isocalendar()
print(result)
print("Year:", result.year)
print("Week:", result.week)
print("Weekday:", result.weekday)
Output
datetime.IsoCalendarDate(year=2024, week=9, weekday=4) Year: 2024 Week: 9 Weekday: 4
4. replace()
It creates a new date object by replacing one or more components of an existing date.
from datetime import date
my_date = date(2024, 2, 29)
new_date = my_date.replace(day=28)
print("Original Date:", my_date)
print("Modified Date:", new_date)
Output
Original Date: 2024-02-29 Modified Date: 2024-02-28
5. toordinal()
This method returns the proleptic Gregorian ordinal of a date.
from datetime import date
my_date = date(2024, 2, 29)
ordinal = my_date.toordinal()
print(ordinal)
Output
738945
6. isoformat()
This method converts a date object into a string following the ISO 8601 date format (YYYY-MM-DD).
from datetime import date
my_date = date(2024, 2, 29)
result = my_date.isoformat()
print(result)
print(type(result))
Output
2024-02-29 <class 'str'>
7. strftime()
It converts a date object into a formatted string using specified format directives.
from datetime import date
my_date = date(2024, 2, 29)
result = my_date.strftime("%d %B %Y")
print(result)
Output
29 February 2024
8. ctime()
This method returns a string representing the date in a predefined format.
from datetime import date
my_date = date(2024, 2, 29)
print(my_date.ctime())
Output
Thu Feb 29 00:00:00 2024
9. timetuple()
This method converts a date object into a time.struct_time object.
from datetime import date
my_date = date(2024, 2, 29)
result = my_date.timetuple()
print(result)
Output
time.struct_time(tm_year=2024, tm_mon=2, tm_mday=29, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=60, tm_isdst=-1)
Date Comparison and Arithmetic
It support comparison and arithmetic operations. These operations help determine the order of dates and calculate differences between them.
Example: Comparing and Subtracting Dates
from datetime import date, timedelta
date1 = date(2024, 2, 28)
date2 = date(2024, 3, 2)
print(date1 < date2)
print(date2 - date1)
print(date1 + timedelta(days=2))
Output
True 3 days, 0:00:00 2024-03-01
Explanation:
- Comparison operators determine which date comes earlier or later.
- Subtracting two date objects returns a timedelta object.
- Adding or subtracting a timedelta changes the date by the specified number of days.