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.

Related Posts

  • Format Date to YYYYMMDD in Python
    03 March

    Format Date to YYYYMMDD in Python

    Table of ContentsDate formats in PythonHow to convert date to YYYYMMDD format in PythonUsing datetime.strftime() Function to Format Date to YYYYMMDD in PythonUsing datetime.date Object to Format Date to YYYYMMDD in PythonUsing date.isoformat() Function to Format Date to YYYYMMDD in PythonUsing arrow.format() Function to Format Date to YYYYMMDD in PythonUsing pendulum.to_date_string() Function to Format Date […]

  • Add seconds to datetime in Python
    11 February

    Add Seconds to Datetime in Python

    Table of ContentsDate and Time in PythonWays to add seconds to datetime object in PythonUsing the datetime.timedelta object to add seconds to datetime in PythonUsing the datetime.time.second attribute to add seconds to datetime in PythonUsing the arrow.shift() function to add seconds to arrow object in PythonConclusion Date and Time in Python Python provides us with […]

  • Python get year from date
    23 January

    Get year from Date in Python

    Table of ContentsDates in PythonWays to get year from date stored in a datetime object in PythonUsing the year attributeUsing the strftime() functionUsing the pandas.to_datetime functionWays to get year from date stored as a string in PythonUsing the for loopUsing the re.search()Conclusion In this post, we will see how to get year from Date in […]

  • Epoch to datetime in Python
    21 August

    Convert epoch to datetime in python

    Table of ContentsUsing the datetime.fromtimestamp() function to convert epoch to datetimeUsing the pandas.to_datetime() function to convert epoch to datetime in Python Epoch time refers to the time elapsed since time was started measuring on January 1, 1970. Usually, it is measured in seconds. It is also known as UNIX time. In this article, we will […]

  • Convert datetime to date in Python
    20 June

    Convert datetime to date in Python

    Table of ContentsUsing the date() function to convert datetime to date in Python.Using the dt.date function in pandas to convert datetime to date in Python. Date and time in Python are not a pre-defined data type itself. The datetime module can be imported to the Python code and it provides classes used that manipulate and […]

  • Convert unix timestamp to datetime in Python
    21 May

    Convert Unix timestamp to DateTime in python

    Table of ContentsUsing fromtimestamp() FunctionUsing strftime() FunctionUsing to_datetime() Function A UNIX timestamp is defined as the several numbers of seconds between a particular date and the Epoch i.e January 1, 1970 at UTC. In this tutorial, we will see some methods by which we can convert a UNIX timestamp to DateTime in Python. Using fromtimestamp() […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.