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 class
from datetime import date
# extract current local date
today = date.today()
print("Today's date:", today)
# show date in different format
today = today.strftime("%d/%m/%Y")
print("Today's date:", today)

Code explanation

  • 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.

Free Resources