Add days to date in Python

Add days to date in Python

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.

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:

Output:

2021-06-04 00:00:00

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.

Output:

2021-05-26 00:00:00

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:

Output:

2021-05-29

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:

Output:

2021-06-04 00:00:00

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, the DateOffset() 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.

Was this post helpful?

Leave a Reply

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