The Wayback Machine - https://web.archive.org/web/20240710095314/https://www.geeksforgeeks.org/python-datetime-date-class/
Open In App

Python DateTime – Date Class

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 class
from datetime import date
 
# initializing constructor
# and passing arguments in the
# format year, month, date
my_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


Output

Date passed as argument is 2020-12-11

Class Attributes

Let’s see the attributes provided by this class – 

Attribute Name Description
min The minimum representable date
max The maximum representable date
resolution The minimum possible difference between date objects
year The range of year must be between MINYEAR and MAXYEAR
month The range of month must be between 1 and 12
day The range of day must be between 1 and number of days in the given month of the given year

Example 1: Getting min and max representable date 

Python3




from datetime import date
 
# Getting min date
mindate = date.min
print("Min Date supported", mindate)
 
# Getting max date
maxdate = date.max
print("Max Date supported", maxdate)


Output

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 object
Date = date(2020, 12, 11)
 
# Accessing the attributes
print("Year:", Date.year)
print("Month:", Date.month)
print("Day:", Date.day)


Output

Year: 2020
Month: 12
Day: 11

Class Functions

 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 – 

List of all Date Class functions

Function Name  Description
ctime() Return a string representing the date
fromisocalendar() Returns a date corresponding to the ISO calendar
fromisoformat() Returns a date object from the string representation of the date
fromordinal() Returns a date object from the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1
fromtimestamp() Returns a date object from the POSIX timestamp
isocalendar() Returns a tuple year, week, and weekday
isoformat() Returns the string representation of the date
isoweekday() Returns the day of the week as integer where Monday is 1 and Sunday is 7
replace() Changes the value of the date object with the given parameter
strftime() Returns a string representation of the date with the given format
timetuple() Returns an object of type time.struct_time
today() Returns the current local date
toordinal() Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1
weekday() Returns the day of the week as integer where Monday is 0 and Sunday is 6

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 class
today = date.today()
 
print("Today's date is", today)
 
# Converting the date to the string
Str = date.isoformat(today)
print("String Representation", Str)
print(type(Str))


Output

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 class
today = date.today()
 
# Getting Weekday using weekday()
# method
print("Weekday using weekday():", today.weekday())
 
# Getting Weekday using isoweekday()
# method
print("Weekday using isoweekday():", today.isoweekday())
 
# Getting the proleptic Gregorian
# ordinal
print("proleptic Gregorian ordinal:", today.toordinal())
 
# Getting the date from the ordinal
print("Date from ordinal", date.fromordinal(737000))


Output

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



Previous Article
Next Article

Similar Reads

How to convert a Python datetime.datetime to excel serial date number
This article will discuss the conversion of a python datetime.datetime to an excel serial date number. The Excel "serial date" format is actually the number of days since 1900-01-00. The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string r
3 min read
Python DateTime - DateTime Class
DateTime class of the DateTime module as the name suggests contains information on both dates as well as time. Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike the date class, the objects of the DateTime cl
5 min read
Pandas Series dt.date | Extract Date From DateTime Objects
The dt.date attribute extracts the date part of the DateTime objects in a Pandas Series. It returns the NumPy array of Python datetime.date objects, mainly the date part of timestamps without information about the time and timezone. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '201
2 min read
How to Fix - "datetime.datetime not JSON serializable" in Python?
In this article, we are going to learn how to fix the error "datetime.datetime not JSON serializable" in Python. datetime.datetime is a class in the Python datetime module that represents a single point in time. This class is not natively supported by the JSON (JavaScript Object Notation) format, which means that we cannot serialize a datetime.date
4 min read
fromisoformat() Function Of Datetime.date Class In Python
The fromisoformat() function is used to constructs a date object from a specified string that contains a date in ISO format. i.e., yyyy-mm-dd. Syntax: @classmethod fromisoformat(date_string) Parameters: This function accepts a parameter which is illustrated below: date_string: This is the specified ISO format i.e., yyyy-mm-dd date string whose date
2 min read
isocalendar() Function Of Datetime.date Class In Python
The isocalendar() function is used to return a tuple containing ISO Year, ISO Week Number, and ISO Weekday. Note: According to ISO standard 8601 and ISO standard 2015, Thursday is the middle day of a week.Therefore, ISO years always start with Monday.ISO years can have either 52 full weeks or 53 full weeks.ISO years do not have any fractional weeks
2 min read
fromtimestamp() Function Of Datetime.date Class In Python
The fromtimestamp() function is used to return the date corresponding to a specified timestamp. Note: Here the timestamp is ranging from the year 1970 to the year 2038, and this function does not consider leap seconds if any present in the timestamp. This function is a class method. Syntax: @classmethod fromtimestamp(timestamp) Parameters: This fun
2 min read
ctime() Function Of Datetime.date Class In Python
The ctime() function is used to return a string containing the date and time. Syntax: ctime() Parameters: This function does not accept any parameter. Return values: This function returns a string containing the date and time. The format of the string representation: The string is of 24-character length.Week is printed as a three-letter word.Sun, M
2 min read
Isoformat() Function Of Datetime.date Class In Python
The Isoformat() function is used to return a string of date, time, and UTC offset to the corresponding time zone in ISO 8601 format. The standard ISO 8601 format is all about date formats for the Gregorian calendar. This format prescribes that a calendar date needs to be represented using a 4-digit year followed by a two-digit month and a two-digit
3 min read
timetuple() Function Of Datetime.date Class In Python
timetuple() method returns a time.struct time object which is a named tuple. A named tuple object has attributes that may be accessed by an index or a name. The struct time object has properties for both the date and time fields, as well as a flag that specifies whether or not Daylight Saving Time is in effect. The year, month, and day fields of th
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg