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.
date.today()
NA
: This method does not take any argument value.
This method returns a date
type object.
# 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)
Line 2: We imported the date
class from the datetime
module.
Line 4: We used the today()
method to get the current local date. The date.today()
function returns the date
object, which is assigned to the today
variable.
Line 5: We printed the today
variable on the console.
Line 7-8: We can print date
on the console in different formats with the date.strftime() method. It converts the given date
objects to a string according to the specified format as an argument.