Python Date Class

The date class is used to instantiate date objects in Python. When an object of this class is instantiated, it represents a date in the format YYYY-MM-DD. The constructor of this class needs three mandatory arguments year, month, and date.

Python Date class Syntax

class datetime.date(year, month, day)

The arguments must be in the following range –  

  • MINYEAR <= year <= MAXYEAR
  • 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. 

Date object representing data in Python

Initializing the constructor and passing arguments in the format year, month, and date.

Python3




# Python program to
# demonstrate date class
 
# import the date class
from datetime import date
 
my_date = date(1996, 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 1996-12-11

Traceback (most recent call last):
File "/home/ccabfb570d9bd1dcd11dc4fe55fd6ba2.py", line 14, in
my_date = date(1996, 12, 39)
ValueError: day is out of range for month

Traceback (most recent call last):
File "/home/53b974e10651f1853eee3c004b48c481.py", line 18, in
my_date = date('1996', 12, 11)
TypeError: an integer is required (got type str)

Get the Current Date

To return the current local date today() function of the date class is used. today() function comes with several attributes (year, month, and day). These can be printed individually. 

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)


Output

Today's date is 2021-08-19

Get Today’s Year, Month, and Date

We can get the year, month, and date attributes from the date object using the year, month and date attribute of the date class.

Python3




from datetime import date
 
# date object of today's date
today = date.today()
 
print("Current year:", today.year)
print("Current month:", today.month)
print("Current day:", today.day)


Output

Current year: 2021
Current month: 8
Current day: 19

Get Date from Timestamp

We can create date objects from timestamps y=using the fromtimestamp() method. The timestamp is the number of seconds from 1st January 1970 at UTC to a particular date.

Python3




from datetime import datetime
 
# Getting Datetime from timestamp
date_time = datetime.fromtimestamp(1887639468)
print("Datetime from timestamp:", date_time)


Output

Datetime from timestamp: 2029-10-25 16:17:48

Convert Date to String

We can convert date object to a string representation using two functions isoformat() and strftime().

Python3




from datetime import date
   
# calling the today
# function of date class
today = date.today()
   
# Converting the date to the string
Str = date.isoformat(today)
print("String Representation", Str)
print(type(Str))


Output

String Representation 2021-08-19
<class 'str'>

List of Date Class Methods

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 an 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

Python datetime module

In Python, date and time are not data types of their own, but a module named DateTime in Python can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. 

In this article, we will explore How DateTime in Python works and what are the main classes of DateTime module in Python.

Table of Content

  • Python DateTime module
  • Python Date Class
  • Python Time class
  • Python Datetime class
  • Python Timedelta Class
  • Python DateTime.tzinfo()
  • Python DateTime timezone

Similar Reads

Python DateTime module

Python Datetime module supplies classes to work with date and time. These classes provide several functions to deal with dates, times, and time intervals. Date and DateTime are an object in Python, so when you manipulate them, you are manipulating objects and not strings or timestamps....

Python Date Class

The date class is used to instantiate date objects in Python. When an object of this class is instantiated, it represents a date in the format YYYY-MM-DD. The constructor of this class needs three mandatory arguments year, month, and date....

Python Time class

...

Python Datetime class

...

Python Timedelta Class

...

Python DateTime.tzinfo()

...

Python DateTime timezone

...