In python, we deal with a lot of date and time-related problems. There is no specific data type for date and time still there is a lot of manipulation done on them in python. We can perform many tasks related to date and time by importing in-built modules and packages. Tasks like conversion of date and time, changing the format of date and time are very common in the world of programming. There is one more task that is equally important and that is adding days to a datein python.
In this tutorial, we will see the different methods by which we can add days to a date in python.
Table of Contents
Using datetime.timedelta()
Function
The datetime
module is a built-in python module by which we can deal with Date functions. The date is present in the form of year, month, day, hour, minute, second as well as microsecond in the datetime
module. With the help of this library, we can manipulate date and time objects simultaneously.
The datetime.timedelta()
function is generally used for date-related manipulation in python. It takes the number of days to be added as the function argument and returns the date. It can also be used for calculating the differences in dates.
Example:
1 2 3 4 5 6 7 |
import datetime initial_date = "05/25/21" initial_date_temp = datetime.datetime.strptime(initial_date, "%m/%d/%y") final_date = initial_date_temp + datetime.timedelta(days=10) print(final_date) |
Output:
Note that strptime()
function is used in the above code. This function takes the initial date as its input. After that, it returns the same date in the datetime.datetime
format. Finally, the datetime.timedelta()
is used to add 10 days to the initial date.
Add one day to date using datetime.timedelta()
Use days=1
with timedelta
function to add one day to date in Python.
1 2 3 4 5 6 7 |
import datetime input_date = "05/25/21" input_date_temp = datetime.datetime.strptime(input_date, "%m/%d/%y") one_date_plus = input_date_temp + datetime.timedelta(days=1) print(one_date_plus) |
Output:
Using timedelta
method from datetime
module
In the timedelta()
method, the number of days to be added in the initial date is taken as an argument and returns it in the date format.
In this method, we also use one more module that is the date
module. This module has a function called today()
which basically returns the present date.
Example:
1 2 3 4 5 |
from datetime import timedelta, date Final_date = date.today() + timedelta(days=10) print(Final_date) |
Output:
Using Pandas
Module
Pandas is one of the most common modules in python. This module helps in dealing with structured and time-series data in a more fast and efficient manner. This module is highly used in data analysis in python.
There is a function called the to_datatime()
function that takes a date as an initial date and converts it into a pandas._libs.tslibs.timestamps.Timestamp
object.
There is one more function called the DateOffset()
function that takes days, months as its argument and returns a pandas.tseries.offsets.DateOffset
object.
Example:
1 2 3 4 5 6 |
import pandas as pd initial_date = "05/25/21" final_date = pd.to_datetime(initial_date) + pd.DateOffset(days=10) print(final_date) |
Output:
Note that, in this method, the initial date is passed as a string. The
to_datetime()
function takes the initial date as an argument and converts it into the DateTime format. Finally, theDateOffset()
function is used to add 10 days to the initial date and the final date is printed
That’s all about how to add days to date in Python.