How To Add Days To A Date In Python

Add days to a date in Python

You can use DateOffset() method to add days to a date in Python. This is basic knowledge for you to start with Python. Let’s learn the specific steps with the explanations and examples below.

Add days to a date in Python

Using DateOffset()

This method works just like the keyword argument form of relativedelta. Note that relativedelta does not support positional argument forms. 

Syntax:

DateOffset(**kwds)

Parameter:

  • kwds: Temporal parameters you want to do with the offset value. If you want to add the days (e.g add 1 day), you can provide “days = 1”.

The relativedelta type is intended to represent a time interval. It can also be used on the DateTime object to change properties of that DateTime. The following example adds 20 days to the DateTime object converted from the given date string.

import pandas as pd
from datetime import datetime

dateString = '01/10/2022'
print('Old Date: ', dateString)

# Create datetime object from the date string
formatDate = '%d/%m/%Y'
dateObject = datetime.strptime(dateString, formatDate)

# Add 20 days to the date
dateObject = dateObject + pd.DateOffset(days=20)
print('New Date: ', dateObject.date())
dateString = dateObject.strftime(formatDate)
print('Formatted New Date: ', dateString)

Output: 

Old Date:  01/10/2022
New Date:  2022-10-21
Formatted New Date:  21/10/2022

The strftime() function is used to convert a string to a DateTime object. This function requires a string representing the date format of the date string. This format depends on your date string, such as year, month, day of week, and date [(%y/%Y – year), (%m/%M – month), (%d – month day)].

In our case, it is (“%Y”), which resembles a year. It prints out the whole year with the century (e.g., 2022).

Using timedelta()

Python timedelta is a module that represents the time difference between two points in time. The time module has functions that allow you to calculate the number of days or weeks elapsed from one point to another.

Syntax:

timedelta(**kwds)

Parameter:

  • kwds: Temporal parameters you want to represent the time difference. If you want to represent the number of days (e.g 1 day), you can provide “days = 1”.

If the given date is in string format, we need to convert it to a DateTime object. For this, we can use the datetime.strptime() function. On the other hand, if the specified date is already a datetime object, this step can be skipped. In the example below, we will add n days (you specified the value n) to our date:

import pandas as pd
from datetime import datetime,timedelta,date

dateString = '01/10/2022'
print('Old Date: ', dateString)

# Create datetime object from date string
formatDate = '%d/%m/%Y'

print("How many days to add ? ")
n = input()
dateObject = datetime.strptime(dateString, formatDate)

# Add n days to the date
dateObject = dateObject + timedelta(days=int(n))
print('New Date: ', dateObject.date())
dateString = dateObject.strftime(formatDate)
print('Formatted New Date: ', dateString)

Output:

Old Date:  01/10/2022
How many days to add ? 
9
New Date:  2022-10-10
Formatted New Date:  10/10/2022

The above code asks the user to type the number of days they want to add to the date object, 1st October 2022. However, if this is not the date you want, you could consider using the current day:

import pandas as pd
from datetime import datetime,timedelta,date

dateObject = datetime.today()
print('Old Date: ', dateObject.date())
formatDate = '%d/%m/%Y'
print("How many days to add ? ")
n = input()

# Add n days to date object
dateObject = dateObject + timedelta(days=int(n))
print('New Date: ', dateObject.date())
dateString = dateObject.strftime(formatDate)
print('Formatted New Date: ', dateString)

Output:

Old Date:  2022-10-01
How many days to add ? 
55
New Date:  2022-11-25
Formatted New Date:  25/11/2022

Check carefully if you have the following classes: date, DateTime, and timedelta from the imported datetime module. Moreover, remember that the parameter must be “days” instead of “day” (this won’t return the error but incorrect results), as the “day” parameter means resetting the actual day to that day.

Summary

We have learned how to add days to a date in Python using two different methods. It would help if you considered each approach’s pros and cons. We hope you will be successful using our tutorial.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *