Python DateTime - Date Class - GeeksforGeeks

19 Dec, 2021
The object of the Date class represents the naive date containing year, month, and date according to the current Gregorian calendar. This date can be extended indefinitely in both directions. The January 1 of year 1 is called day 1 and January 2 or year 2 is called day 2 and so on.
Syntax:
class datetime.date(year, month, day)
The arguments must be in the following range –
MINYEAR(1) <= year <= MAXYEAR(9999)
1 <= month <= 12
1 <= day <= number of days in the given month and year
Note: If the argument is not an integer it will raise a TypeError and if it is outside the range a ValueError will be raised. 
Example:
Python3
# Python program to# demonstrate date class # import the date classfrom datetime import date # initializing constructor# and passing arguments in the# format year, month, datemy_date = date(2020, 12, 11) print("Date passed as argument is", my_date) # Uncommenting my_date = date(1996, 12, 39)# will raise an ValueError as it is# outside range # uncommenting my_date = date('1996', 12, 11)# will raise a TypeError as a string is# passed instead of integer
Date passed as argument is 2020-12-11
Let’s see the attributes provided by this class – 
Example 1: Getting min and max representable date 
Python3
from datetime import date # Getting min datemindate = date.minprint("Min Date supported", mindate) # Getting max datemaxdate = date.maxprint("Max Date supported", maxdate)
Min Date supported 0001-01-01
Max Date supported 9999-12-31
Example 2: Accessing the year, month, and date attribute from the date class
Python3
from datetime import date # creating the date objectDate = date(2020, 12, 11) # Accessing the attributesprint("Year:", Date.year)print("Month:", Date.month)print("Day:", Date.day)
Year: 2020
Month: 12
Day: 11
 Date class provides various functions to work with date object, like we can get the today’s date, date from the current timestamp, date from the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1, etc. Let’s see the list of all the functions provided by this class – 
Let’s see certain examples of the above functions
Example 1: Getting the current date and also change the date to string
Python3
# Python program to# print current date from datetime import date # calling the today# function of date classtoday = date.today() print("Today's date is", today) # Converting the date to the stringStr = date.isoformat(today)print("String Representation", Str)print(type(Str))
Today's date is 2021-07-23
String Representation 2021-07-23
<class 'str'>
Example 2: Getting the weekday from the day and proleptic Gregorian ordinal
Python3
# Python program to# print current date from datetime import date # calling the today# function of date classtoday = date.today() # Getting Weekday using weekday()# methodprint("Weekday using weekday():", today.weekday()) # Getting Weekday using isoweekday()# methodprint("Weekday using isoweekday():", today.isoweekday()) # Getting the proleptic Gregorian# ordinalprint("proleptic Gregorian ordinal:", today.toordinal()) # Getting the date from the ordinalprint("Date from ordinal", date.fromordinal(737000))
Weekday using weekday(): 4
Weekday using isoweekday(): 5
proleptic Gregorian ordinal: 737994
Date from ordinal 2018-11-02
Note: For more information on Python Datetime, refer to Python Datetime Tutorial
singghakshay
simranarora5sos
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()