Categories
Python Answers

How to add days to a date in Python?

Spread the love

Sometimes, we want to add days to a date in Python.

In this article, we’ll look at how to add days to a date in Python.

How to add days to a date in Python?

To add days to a date in Python, we can use the datetime.timedelta function.

For instance, we write

import datetime

date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=10)

to create a date from the start_date date string with strptime.

Then we create a time delta object with timedelta with the days argument set to the number of days we want to add.

Then we add the time delta object to date_1 to return a new date with 10 days added to date_1.

Conclusion

To add days to a date in Python, we can use the datetime.timedelta function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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