In this article, we will discuss different ways to subtract days from a given date in python.
Suppose we have a date ’21/1/2021′ and we want to subtract N days to it and N can be 1, 7, 300 or any other number. We want the final date after subtracting N days from the given date. Let’s see how to do that,
Subtract days from a date in Python using timedelta & datetime
Python provides a module datetime for manipulation of date and time. It consists of following classes,
- datetime.date: An object of date class specifies a date using year, month and day
- datetime.time: An object of time class specifies a timestamp using hour, minute, second, microsecond, and tzinfo.
- datetime.datetime: An object of datetime is a combination of a date and a time.
- datetime.timedelta: A duration, that represents the difference between two dates or times or datetimes.
- datetime.tzinfo: It contains the timezone information.
To subtract days from a given date, we are going to use the datetime and timedelta classes of the datetime module of python.
Steps to subtract N days to date are as follows,
- Step 1: If the given date is in a string format, then we need to convert it to the datetime object. For that we can use the datetime.strptime() function. Whereas, if the given date is already a datetime object, then you can skip this step.
- Step 2: Create an object of timedelta, to represent an interval of N days. For that, pass the argument days with value N in the timedelta constructor.
- Step 3: Subtract the timedelta object from the datetime object. It will give us a datetime object pointing to a date i.e. N days before the given date.
- Step 4: If you want the final date in string format, then convert the datetime object to string using strftime(). You can pass the format string as argument and it will convert the datetime object to a string of the specified format.
Let’s understand with an example,
Subtract 10 days from a date in python using timedelta
from datetime import datetime from datetime import timedelta given_date = '21/1/2021' print('Given Date: ', given_date) # Create datetime object from date string date_format = '%d/%m/%Y' dtObj = datetime.strptime(given_date, date_format) # Subtract 10 days from a given datetime object n = 10 past_date = dtObj - timedelta(days=n) print('Past Date: ', past_date) print('Past Date: ', past_date.date()) past_date_str = past_date.strftime(date_format) print('Past Date as string object: ', past_date_str)
Output
Given Date: 21/1/2021 Past Date: 2021-01-11 00:00:00 Past Date: 2021-01-11 Past Date as string object: 11/01/2021
We subtracted 10 days in the date ’21/1/2021′ to make it ’11/01/2021′.
Frequently Asked:
- Convert Local datetime to UTC timezone in Python
- Check if Date is Older than 30 Days in Python
- Convert String to datetime in Python
- How to add minutes to datetime in python?
As we subtracted timedelta (of 10 day duration) from the datetime object, so it returned a new datetime object pointing to the new date. As datetime object has the the timestamp also, therefore it also got printed. If you want date only, then you can fetch the date object from datetime object using date() function, just like we did in above example. In the end we converted the datetime object to the required string format using datetime.strftime().
Subtract days from a date in Python using Pandas
Pandas provide a class DateOffset, to store the duration or interval information. It is mostly used to increment or decrement a timestamp. It can be used with datetime module to to subtract N days from a date.
Let’s understand with an example,
Subtract 100 days from a date in python
from datetime import datetime import pandas as pd given_date = '1/21/2021' print('Given Date: ', given_date) # Create datetime object from date string date_format = '%m/%d/%Y' dtObj = datetime.strptime(given_date, date_format) # Subtract 100 days from a given datetime object n = 100 past_date = dtObj - pd.DateOffset(days=n) print('Past Date: ', past_date) print('Past Date: ', past_date.date()) past_date_str = past_date.strftime(date_format) print('Past Date as string object: ', past_date_str)
Output
Given Date: 1/21/2021 Past Date: 2020-10-13 00:00:00 Past Date: 2020-10-13 Past Date as string object: 10/13/2020
We created a DateOffset object by passing days as 100. Then we subtracted this DateOffset object from the datetime object. It returned a datetime object pointing to the date i.e. before 100 days from the given date.
Let’s see some more examples,
Example: Subtract 365 days from a date in python
from datetime import datetime from datetime import timedelta given_date = '1/21/2021' print('Given Date: ', given_date) # Create datetime object from date string date_format = '%m/%d/%Y' dtObj = datetime.strptime(given_date, date_format) # Subtract 365 days from a given datetime object n = 365 past_date = dtObj - timedelta(days=n) print('Past Date: ', past_date) print('Past Date: ', past_date.date()) past_date_str = past_date.strftime(date_format) print('Past Date as string object: ', past_date_str)
Output
Given Date: 1/21/2021 Past Date: 2020-01-22 00:00:00 Past Date: 2020-01-22 Past Date as string object: 01/22/2020
We subtracted 365 days to the ‘1/21/2021′ and the final date we got is ’01/22/2020’.
Example: Subtract 60 days from a date in python
from datetime import datetime from datetime import timedelta given_date = '21/1/2021' print('Given Date: ', given_date) # Create datetime object from date string date_format = '%d/%m/%Y' dtObj = datetime.strptime(given_date, date_format) # Subtract 60 days from a given datetime object n = 60 past_date = dtObj - timedelta(days=n) print('Past Date: ', past_date) print('Past Date: ', past_date.date()) past_date_str = past_date.strftime(date_format) print('Past Date as string object: ', past_date_str)
Output
Given Date: 21/1/2021 Past Date: 2020-11-22 00:00:00 Past Date: 2020-11-22 Past Date as string object: 22/11/2020
We subtracted 60 days to the ’21/1/2021′ and the final date we got is ’22/11/2020′.
Example: Subtract 14 days from a date in python
from datetime import datetime from datetime import timedelta given_date = '21/1/2021' print('Given Date: ', given_date) # Create datetime object from date string date_format = '%d/%m/%Y' dtObj = datetime.strptime(given_date, date_format) # Subtract 14 days from a given datetime object n = 14 past_date = dtObj - timedelta(days=n) print('Past Date: ', past_date) print('Past Date: ', past_date.date()) past_date_str = past_date.strftime(date_format) print('Past Date as string object: ', past_date_str)
Output
Given Date: 21/1/2021 Past Date: 2021-01-07 00:00:00 Past Date: 2021-01-07 Past Date as string object: 07/01/2021
We subtracted 14 days to the ‘1/21/2021′ and the final date we got is ’07/01/2021’.
Summary:
We learned about different ways to subtract N days from a date in python.