What is the datetime date.today() function in Python?
Overview
In Python, there are multiple ways to extract the current date. In this shot, we will use the today() method from the date class of the datetime module.
The
datetime.now()method can extract the current date and time.
Syntax
date.today()
Parameters
NA: This method does not take any argument value.
Return value
This method returns a date type object.
Code example
# import date classfrom datetime import date# extract current local datetoday = date.today()print("Today's date:", today)# show date in different formattoday = today.strftime("%d/%m/%Y")print("Today's date:", today)
Code explanation
-
Line 2: We imported the
dateclass from thedatetimemodule. -
Line 4: We used the
today()method to get the current local date. Thedate.today()function returns thedateobject, which is assigned to thetodayvariable. -
Line 5: We printed the
todayvariable on the console. -
Line 7-8: We can print
dateon the console in different formats with the date.strftime() method. It converts the givendateobjects to a string according to the specified format as an argument.