Javaexercise.com

Adding Days To A Date In Python

Python is a high-level, interpreted programming language, designed and developed by Guido van Rossum in the 1990s. It is one of the most popular programming languages present in the software industry.

One of the most important concepts in python is that of adding days to a date in python.

Let us consider an example where we have an arbitrary starting date, say the 30th of March in the year 2022 and we need to add 5 days to this date.

The resulting date should be the 4th of April of the year 2022. Note that the month of March has 31 days in total.

Let us have a look at the different ways of doing this in detail.

Adding days by using the date and timedelta libraries in Python

In this method, we use the date and timedelta libraries to add days to a date. To work with these libraries we must first import them using the import statement.

We define a variable start_date that stores the starting date. The desired starting date in this example is the 30th of March in the year 2022.

This is specified using the date() function with the parameters year, month and day passed as 2022, 3, and 30 respectively.

Next, we add 5 days to this starting date using the timedelta() function with the parameter days passed as 5.

The result is stored in the variable new_Date and later printed.

  Let us look at the Python 3 code and corresponding output for this method - 

# Importing the required libraries
from datetime import date
from datetime import timedelta

# Starting date
start_date = date(year = 2022, month = 3, day = 30)

# Performing the operation
new_date = start_date + timedelta(days = 5)

# Printing
print(new_date)

Output

2022-04-04

Instead of using the date library, we can also use the datetime library.

The only difference here is that the result is stored as a datetime object instead of a date object and the data displayed is along with time stamps as well.

The default timestamp is 00:00:00. Let us look at the Python 3 code and corresponding output for this method - 

# Importing the required libraries
from datetime import datetime
from datetime import timedelta

# Starting date
start_date = datetime(year = 2022, month = 3, day = 30)

# Performing the operation
new_date = start_date + timedelta(days = 5)

# Printing
print(new_date)

Output

2022-04-04 00:00:00

Adding days by using the date and pandas libraries in Python

In this method, we use the date and pandas libraries to add days to a date.

We define a variable start_date that stores the starting date.

The desired starting date in this example is 30th of March in the year 2022. This is specified using the date() function with the parameters year, month and day passed as 2022, 3, and 30 respectively.

Next, we add 5 days to this starting date using the Day() function we had imported earlier and multiply it by the number of days to be added, here 5.

The result is stored in the variable new_date. We need to extract only the date part out of it for which we use the new_date.date() function and reassign the returned object to the new_date variable, which is later printed.

Let us look at the Python 3 code and corresponding output for this method:

# Importing the required libraries
from datetime import date
from pandas.tseries.offsets import Day

# Starting date
start_date = date(year = 2022, month = 3, day = 30)

# Performing the operation
new_date = start_date + 5*Day()
new_date = new_date.date()

# Printing
print(new_date)

Output

2022-04-04

Conclusion

In this topic, we have learned the use and advantages of adding days to a date in a Python program, following some simple running examples of programs, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.