How to Add Days to a Date in Python?

There is an inbuilt “datetime” module in Python used to manipulate dates or times. The date and time are returned by the “datetime” module in a “YYYY-MM-DD HH:MM:SS” format. To add days to date in Python, the “timedelta()” class, “date” and “pd.dateoffset()” functions are used.

The following methods are utilized to add days to a specific or current date in Python:

Method 1: Using timedelta() Class of datetime

The “timedelta()” class is used in Python to calculate the positive and negative date difference and represent a duration. The following demonstrates how Python can add the days to a specific date.

Example 1: Adding Days to a Specific Date in Python

In the code given below, the “datetime” module function “timedelta()” is sued to add specific days to any given date:

Code:

import datetime
date_input = datetime.datetime(2024, 11, 6)
print(date_input)
addded_days = datetime.timedelta(days = 14)
output = date_input + addded_days
print(output)

In the above code:

  • The “datetime.datetime()” function accepts the specific date as an argument and returns the date in a valid DateTime format.
  • The “timedelta()” class is used to add the specific days “14” to the given DateTime.

Output:

The above output shows that the specified number of days, i.e., “14,” has been added to the given date.

Example 2: Adding Days to a Current Date in Python

In the code below, the “timedelta()” class is used to add the days to today/current date:

Code:

import datetime
today_date = datetime.date.today()
print(today_date)
added_days = datetime.timedelta(days=14)
output = today_date + added_days
print(output)

In the above code:

  • The “datetime.date.today()” function is utilized to get the current date.
  • The “timedelta()” accepts the specific days “14” as a parameter and stores the date in the variable “added_days”.
  • The “added_days” and “today_date” are added to get the final date after addition.

Output:

The above output shows the given date, i.e., “2023-01-17,” and the modified date, i.e., “2023-01-31

Method 2: Using Pandas Module

The Pandas module provides a function named “pd.Dateoffset()” to add the specific days to the given date in Python. Here is a code example:

Code:

import pandas as pd
date_val = "2022-12-12"
print(date_val)
adding_days = pd.DateOffset(days=36)
output = pd.to_datetime(date_val) + adding_days
print(output)

In the above code:

  • The specific date is stored in a string variable “date_val”.
  • The “pd.Dateoffset()” function is used to add the specific date “16” to the given date and store the date in the variable “adding_days”.
  • The “pd.to_datetime()” function converts the input date string into date type and adds the specific days to the given date.

Output:

The above output shows the given date and date after adding “36” days.

Conclusion

To add days into a date, the “timedelta()” class of the “datetime” module and “pd.Dateoffset()” of the panda’s module are used in Python. The “timedelta()” class adds the unique number of days to the current date or adds days to any given “date”. The “pd.Dateoffset()” function of the panda’s module is used to move forward or backward dates according to specific days. Using various examples, this guide explained different Python methods to add days to date.