Subtract Days, Months & Years from datetime Object in Python (3 Examples)

 

In this tutorial, I’ll illustrate how to get X days, months, and years earlier as a certain datetime object in Python.

Table of contents:

Let’s get started…

 

Example Data & Add-On Libraries

We first need to import the datetime module:

import datetime                                           # Import datetime module to Python

Furthermore, we need to import the relativedelta function, as you can see below:

from dateutil.relativedelta import relativedelta          # Import relativedelta

Now, we are ready to create an example datetime object:

my_datetime = datetime.datetime(2023, 6, 20, 7, 35, 18)   # Constructing example datetime
print(my_datetime)                                        # Return example datetime
#2023-06-20 07:35:18

 

Example 1: Get datetime X Days Ago

In the first example, I’ll explain how to subtract a specific number of days from our datetime object.

For this task, we can use the relativedelta function and the days argument, as you can see here:

my_date_days = my_datetime - relativedelta(days = 12)     # Calculate 12 days earlier
print(my_date_days)                                       # Return new datetime object
# 2023-06-08 07:35:18

The previous output of the Python console shows that we have created a new data object called my_date_days, which shows our input date and time 12 days earlier.

 

Example 2: Get datetime X Months Ago

In Example 2, I’ll demonstrate how to create a date and time object that is X months before our input data.

To get there, we can apply the relativedelta function once again, but his time combined with the months argument.

my_date_months = my_datetime - relativedelta(months = 4)  # Calculate 4 months earlier
print(my_date_months)                                     # Return new datetime object
# 2023-02-20 07:35:18

The new datetime object called my_date_months has been created and shows the date 4 months earlier than in our input date.

 

Example 3: Get datetime X Years Ago

The last example explains how to get a date object, which returns our example date X Years ago.

To accomplish this, we simply apply the relativedelta function within the years argument:

my_date_years = my_datetime - relativedelta(years = 8)    # Calculate 8 years earlier
print(my_date_years)                                      # Return new datetime object
# 2015-06-20 07:35:18

Considering the output above, the date in the new datetime object is 8 years earlier compared to the input object.

 

Video, Further Resources & Summary

Would you like to learn more about the subtraction of a particular number of days, months, and years from a datetime? Then I recommend watching the following video on my YouTube channel. I illustrate the Python code of this article in the video:

 

The YouTube video will be added soon.

 

Furthermore, you might want to have a look at some other articles on this website. You can find a selection of articles on related topics such as dates, time objects, and data objects below.

 

To summarize: In this tutorial, you have learned how to subtract a specific number of days, months, and years from a date and time object to get a previous date and time in the Python programming language. Let me know in the comments, in case you have further comments and/or questions. Furthermore, please subscribe to my email newsletter in order to get updates on the newest articles.

 

Matthias Bäuerlen Python Programmer

This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top