How to get yesterday date in Python?

Ads
 

How to get yesterday date in Python?

In this tutorial we are going to develop program in Python for getting the yesterday's date. You will learn how to get yesterday's date in the Python program?

In this tutorial we are going to develop program in Python for getting the yesterday's date. You will learn how to get yesterday's date in the Python program?

Yesterday's date in Python - How to get yesterday date in Python?

Python is very versatile programming language that comes with the many ready to use API. In this article we will learn how to get yesterday's date in the Python program. We are going to use the datetime library of Python for getting the today's date and then subtract one day from the current date to get the yesterday's date.

The Python datetime classes are distributed with the default distribution and it can be used without any other package installation. Python datetime module comes with the classes for working with the dates and times. Classes in this package is used in today's program for getting the yesterday's date.

In this program we are going to use the datetime class from datetime for getting the today's date and also used the timedelta() method of this class.

In our program we will get the current date with the help of datetime.datetime.today() method and after that substract one date with the help of datetime.timedelta(days=1) function. So, this way you can easily get yesterday date in your Python program.

Here is the complete program for getting the yesterday date in Python:



import datetime

dtToday = datetime.datetime.today()

print (dtToday)

dtYesterday = dtToday - datetime.timedelta(days=1)

print(dtYesterday)

Here is the screenshot of the program:

Python Yesterday Date Example

In this example we have used datetime and timedelta  classes. Here is more details about these classes.

datetime: This class represents date and time along with the other important attributes. The attributes includes are year, month, day, hour, minute, second, microsecond, and tzinfo; which is very useful in programming.

timedelta: This class is used to get the duration in date, time, or datetime instances to microsecond resolution in the date object. This is useful in adding or subtracting from the date object in Python.

How to get yesterday date in Python in yyyymmdd format?

Now we will learn to get yesterday date in yyyymmdd format. After getting the yesterday's date you can format it in the yyyymmdd format.

To convert the date in the yyyy-mm-dd we will use strftime() method. Here is the complete program:


import datetime

dtToday = datetime.datetime.today()

print (dtToday)

dtYesterday = dtToday - datetime.timedelta(days=1)

print(dtYesterday)

yesterday = dtYesterday.strftime('%Y-%m-%d')

print(yesterday)

yesterday = dtYesterday.strftime('%Y-%m-%d %H:%M:%S')

print(yesterday)

If you run the program you will get following output:

C:\Users\Nick>python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>>
>>> dtToday = datetime.datetime.today()
>>>
>>> print (dtToday)
2022-02-11 09:15:42.068758
>>>
>>> dtYesterday = dtToday - datetime.timedelta(days=1)
>>>
>>> print(dtYesterday)
2022-02-10 09:15:42.068758
>>>
>>> yesterday = dtYesterday.strftime('%Y-%m-%d')
>>>
>>> print(yesterday)
2022-02-10
>>>
>>> yesterday = dtYesterday.strftime('%Y-%m-%d %H:%M:%S')
>>>
>>> print(yesterday)
2022-02-10 09:15:42
>>>
>>>

Here is the screenshot of the program execution:

Python Yesterday Date Example

In this tutorial we have learned to get yesterday's date in Python and then format it in YYYMMDD format. Python datetime classes are very helpful in working with the Date and Time. Developers uses this library to work with the date and perform various date conversion and formatting operations.

More Tutorials:

Ads

Ads