Python

Datetime Get Previous Month Python

While programming in Python, you may want to get the previous month’s date and time. Python provides multiple modules and functions to get it quickly and efficiently. The “datetime” module is one of them that is commonly utilized to work with date and time. This module is included by default in Python, so we are not required to install it separately.

This post will discuss getting the previous month’s date and time in Python.

How to Get the Datetime of the Previous Month in Python?

To get the datetime of the previous month in Python, the following techniques are used:

Method 1: Get the Datetime of Previous Month in Python Using “datetime” Module With “replace()” Method

To get the datetime of the previous month in Python, the “datetime” module with the “replace()” method can be used. The “datetime” module is the built-in package of Python that contains classes for handling dates and times.

Example

At first, import the “date” module from the “datetime” library:

from datetime import date

Now, create a variable that stores the current date, month, and year with the “date” method:

c_date = date(2023, 2, 1)

Use the “if” condition with month and years to check if the provided condition is equal to the “1”. If not, then subtract the “1” from the year and current date and store them to the “month” and “year”:

month, year = (c_date.month-1, c_date.year) if c_date.month != 1 else (12, c_date.year-1)

Now, call the “replace()” method with day, month, and year values as arguments and store them to the p_month“ variable:

p_month = c_date.replace(day=1, month=month, year=year)

Use the print statement to view the previous month’s date:

print(p_month)

Output

Method 2: Get the Datetime of Previous Month in Python Using “datetime” Module With Extension “dd”

To get the datetime of the previous month in Python, the “datetime” module can be used by importing the “dd” extension. Now, check the provided example for a more understanding.

Example

Initially, Import the “datetime” module by utilizing the “dd” extension:

import datetime as dd

Now, get the current time and date, and call the “dd.datetime.today()” method:

c_date = dd.datetime.today()

Call the “replace()” method inside the “if” condition, if the current month is “1”, then replace it with the “c_date” as “dd.datetime.year – 1”. Otherwise, set the “days” variable to “0”. You also use the “try” and “catch” blocks. If any exception occurs and print the previous month date and time:

ifc_date.month == 1:
p_month = c_date.replace(year=c_date.year - 1, month=12)
else:
    days = 0
    while True:
        try:
p_month = c_date.replace(month=c_date.month - 1, day=c_date.day - days)
            break
        except ValueError:
           days += 1
print(p_month)

As you can see, the below-given output contains the previous month date and time:

That’s it! We have elaborated how to get the previous month datetime in Python.

Conclusion

To get the previous month’s datetime in Python, the “datetime” module with the “replace()” method and the “datetime” module using the extension “dd” techniques are used. The “datetime” module contains classes for handling dates and times. It can be utilized by importing the “dd” extension. The “replace()” method checks the provided condition and replaces the value according to user desire. This post elaborated on the ways of getting the previous month datetime in Python.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.