Get Today’s Date in Python

Get today's date in Python

Date and Time objects in Python

In Python, we can work efficiently with date and time values. The datetime module is used to create datetime objects that can store date and time values. We can work with different functionalities with such objects. Apart from the datetime module, we also have other packages like the arrow that can also store the +date and time values.

Get Today’s Date in Python

In this article, we will return the current date using different methods in Python.

Using date.today() Function to Get Today’s Date in Python

In Python, everyone commonly uses the datetime module to work with the date and time values. We can create an object of the date class that stores the year, month, and day of a given date.

The date.today() function will return a datetime.date object containing the current date. We can use this method to get today’s date in Python.

For example,

Output:

2022-02-20

Using datetime.today() and datetime.strftime() Functions to Get Today’s Date in Python

The datetime.today() function creates a datetime object containing the current date and time. We can extract the date from this object to get today’s date in Python.

We can extract the individual attributes of this object to get today’s date in Python. We will concatenate the value of the year, month, and day attributes.

For example,

Output:

2022-02-20

In the above example,

  • We combine the individual attributes from the datetime object to get today’s date in Python.
  • We convert each attribute to a string using the str() function.

Using datetime.now() Function to Get Today’s Date in Python

The datetime.now() function in Python is used to return the local date and time based on a timezone. We can specify the timezone in the function.

This timezone is Greenwich Meridian time by default.

We will get the date using the same approach we used in the previous method. We will combine the attributes individually.

See the code below.

Output:

2022-02-20

Using strftime() Function to Get Today’s Date in Python

The strftime() function is used to return a string of the datetime object in our desired format. We can specify the format within the function. To extract the date, we will specify the format for the year, month, and day within the function.

We will create a datetime object using the datetime.today() or datetime.now() function and then use this function to get today’s date in Python.

For example,

Output:

2022-02-20

In the above example,

  • We store the current date and time in the object t using the datetime.today() function.
  • We get today’s date from this object using the strftime() function.
  • The final result is a string.

Using time.strftime() Function to Get Today’s Date in Python

The time library allows us to perform different time-related operations in Python. Its constructor returns the current date and time. So, we can use the strftime() function to get today’s date in Python using the time object.

For example,

Output:

2022-02-20

Using arrow.now() Function to Get Today’s Date in Python

The arrow is yet another module that is gaining popularity for its simplicity in storing and working with the date and time values.

Using the arrow.now() function we can get an object that contains the current date and time. We can use the format() function with this object to get today’s date in Python.

We need to specify the format for the date in the format() function.

For example,

Output:

2022-02-20

Using pendulum.today() Function to Get Today’s Date in Python

The pendulum module is also another library that allows us to work with time and date. With the pendulum.today() function, we can return the current date and time.

To extract the date, we will use the to_date_string() function. This function returns a string containing the date from a pendulum date and time object.

For example,

Output:

2022-02-20

Using pendulum.now() Function to Get Today’s Date in Python

The pendulum.now() function also returns the date and time of the current timezone. Using a similar approach with the to_date_string() method, we can get today’s date in Python.

For example,

Output:

2022-02-20

Using pandas.to_datetime() Function to Get Today’s Date in Python

The pandas module allows us to work with DataFrames. The module itself has different functionalities to work with timestamp values as these values may be encountered in the DataFrame.

The pandas.to_datetime() function creates a pandas.Timestamp object. If we provide an argument today in the function, then it returns a pandas.Timestamp object containing today’s date and time.

We can get today’s date in Python from this object.

We can use the date() attribute to get the date from this object in a datetime object.

For example,

Output:

2022-02-20

We can also use the strftime() function and specify the format for the date to return the date from this pandas.Timestamp object as a string.

See the code below.

Output:

2022-02-20

Using pandas.Timestamp() Function to Get Today’s Date in Python

We can also use the Timestamp() function to create pandas.Timestamp() objects. Then we can return the date from this objec using the strftime() or date() function.

Output:

2022-02-20

Conclusion

In this tutorial, we discussed how to get today’s date in Python. For this, first, we demonstrated different methods associated with the datetime module. We also discussed how to use the time, arrow, and pendulum libraries to get today’s date in Python. The pandas module was also discussed and how to retrieve the date from the timestamp objects of this module.

Was this post helpful?

Related Posts

  • 23 September

    TypeError: Object of Type Datetime Is Not Json Serializable in Python

    Table of ContentsFix the object of type datetime is not JSON serializable exception in PythonUsing the default parameter in the json.dumps() functionUsing the cls parameter in the json.dumps() functionUsing the str functionConclusion In Python, the datetime library allows us to create objects of the datetime class that can store and process date and time values […]

  • 23 September

    Convert Datetime to Unix Timestamp in Python

    Table of ContentsDates and Unix TimestampWays to convert datetime to Unix timestampUsing the total_seconds() function to convert datetime to Unix timestamp in PythonUsing the timestamp() function to convert datetime to Unix timestamp in PythonUsing the strftime() function to convert datetime to Unix timestamp in PythonUsing the time.mktime() function to convert datetime to Unix timestamp in […]

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 18 May

    Get Hour from datetime in Python

    Table of ContentsHow to get hour from datetime in Python?Using the time() function and manually slicing out the hour part to get hour from datetime in Python.Using the % operator along with the datetime.hour attribute to get hour from datetime in Python.Using the format() function along with the datetime.hour attribute to get hour from datetime […]

  • 18 May

    Convert Month Name to Number in Python

    Table of ContentsHow to convert month name to number in Python?Creating a user-defined function and utilizing it to convert month name to number in Python.Using the calendar module to create a dictionary and utilize it to convert month name to number in Python.Using dict comprehension and the calendar module to convert month name to number […]

  • 18 May

    Add Hours to datetime in Python

    Table of Contentsdatetime in PythonWays to add hours to datetime in PythonUsing the datetime.Hour Attribute to Add Hours to datetime in PythonUsing the datetime.timedelta object to add hours to datetime in PythonUsing the dateutil.Relativedelta Object to Add Hours to datetime in PythonUsing the pandas.Date Offset() Function to Add Hours to datetime in PythonUsing the arrow.shift() […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.