In this article, we will discuss different ways to add days to a given date in python.

Suppose we have a date ’21/1/2021′ and we want to add N days to it and N can be 1, 10, 100 or any other number. We want the final date after adding N days in the given date. Let’s see how to do that,

Add days to a date in Python using timedelta

Python provides a datetime module 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 add days in a given date, we are going to use the datetime and timedelta classes of the datetime module of python.

Steps to add 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 class, to represent an interval of N days. For that, pass the argument days with value N in the timedelta constructor.
  • Step 3: Add the timedelta object to the datetime object. It will give us a datetime object, pointing to a date i.e. N days after 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,

Add 10 days to 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)

# Add 10 days to a given datetime object
n = 10
future_date = dtObj + timedelta(days=n)

print('Future Date: ', future_date)
print('Future Date: ', future_date.date())

future_date_str = future_date.strftime(date_format)
print('Future Date as string object: ', future_date_str)

Output

Given Date:  21/1/2021
Future Date:  2021-01-31 00:00:00
Future Date:  2021-01-31
Future Date as string object:  31/01/2021

We added 10 days in the date ’21/1/2021′ to make it ‘2021-01-31’.

As we added timedelta (of 10 day duration) to the datetime object, so it returned a new datetime object pointing to the final 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().

How to add days to 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 add N days to a date.

Let’s understand with an example,

Add 100 days to a date in python using pandas

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)

# Add 100 days to a given datetime object
n = 100
future_date = dtObj + pd.DateOffset(days=n)

print('Future Date: ', future_date)
print('Future Date: ', future_date.date())

future_date_str = future_date.strftime(date_format)
print('Future Date as string object: ', future_date_str)

Output

Given Date:  1/21/2021
Future Date:  2021-05-01 00:00:00        
Future Date:  2021-05-01
Future Date as string object:  05/01/2021

We created a DateOffset object by passing days as 100. Then we added this DateOffset object to the datetime object. It returned a datetime object pointing to the date i.e. after 100 days from the given date.

Let’s see some more examples,

Add 365 days to 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)

# Add 365 days to a given datetime object
n = 365
future_date = dtObj + timedelta(days=n)

print('Future Date: ', future_date)
print('Future Date: ', future_date.date())

future_date_str = future_date.strftime(date_format)
print('Future Date as string object: ', future_date_str)

Output

Given Date:  1/21/2021
Future Date:  2022-01-21 00:00:00        
Future Date:  2022-01-21
Future Date as string object:  01/21/2022

We added 365 days to the ‘1/21/2021′ and the final date we got is ’01/21/2022’.

Example: Add 60 days to 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)

# Add 60 days to a given datetime object
n = 60
future_date = dtObj + timedelta(days=n)

print('Future Date: ', future_date)
print('Future Date: ', future_date.date())

future_date_str = future_date.strftime(date_format)
print('Future Date as string object: ', future_date_str)

Output

Given Date:  21/1/2021
Future Date:  2021-03-22 00:00:00        
Future Date:  2021-03-22
Future Date as string object:  22/03/2021

We added 60 days to the ’21/1/2021′ and the final date we got is ’22/03/2021′.

Example: Add 14 days to 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)

# Add 14 days to a given datetime object
n = 14
future_date = dtObj + timedelta(days=n)

print('Future Date: ', future_date)
print('Future Date: ', future_date.date())

future_date_str = future_date.strftime(date_format)
print('Future Date as string object: ', future_date_str)

Output

Given Date:  21/1/2021
Future Date:  2021-02-04 00:00:00        
Future Date:  2021-02-04
Future Date as string object:  04/02/2021

We added 14 days to the ‘1/21/2021′ and the final date we got is ’04/02/2021’.

Summary:

We learned about different ways to add N days to a date in python.