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?

Leave a Reply

Your email address will not be published. Required fields are marked *