Add days to a date in Python

avatar

Borislav Hadzhiev

Mon Jun 20 20222 min read

Add days to a date in Python #

Use the timedelta() class from the datetime module to add days to a date, e.g. result_1 = date_1 + timedelta(days=3). The timedelta class can be passed a days argument and adds the specified number of days to the date.

main.py
from datetime import datetime, date, timedelta # ✅ add days to a date my_str = '09-24-2023' # 👉️ (mm-dd-yyyy) date_1 = datetime.strptime(my_str, '%m-%d-%Y') print(date_1) # 👉️ 2023-09-24 00:00:00 result_1 = date_1 + timedelta(days=3) print(result_1) # 👉️ 2023-09-27 00:00:00 # ----------------------------------------------- # ✅ add days to current date current_date = datetime.today() print(current_date) # 👉️ 2022-06-20 09:42:22.341830 result_2 = current_date + timedelta(days=7) print(result_2) # 👉️ 2022-06-27 09:43:09.084770 # ----------------------------------------------- # ✅ using date instead of datetime date_3 = date(2023, 9, 24) print(date_3) # 👉️ 2023-09-24 result_3 = date_3 + timedelta(days=3) print(result_3) # 👉️ 2023-09-27 # ----------------------------------------------- # ✅ add days to current date (using date instead of datetime) date_4 = date.today() print(date_4) # 👉️ 2022-06-20 result_4 = date_4 + timedelta(days=7) print(result_4) # 👉️ 2022-06-27

Make sure to import the datetime or date and timedelta classes from the datetime module.

When using this approach, the month (and year) will roll over if necessary.

The first example uses the datetime.strptime() method to get a datetime object that corresponds to the provided date string, parsed according to the specified format.

Once we have the datetime object, we can use the timedelta class to add days to it.

main.py
from datetime import datetime, timedelta my_str = '09-24-2023' # 👉️ (mm-dd-yyyy) date_1 = datetime.strptime(my_str, '%m-%d-%Y') print(date_1) # 👉️ 2023-09-24 00:00:00 result_1 = date_1 + timedelta(days=3) print(result_1) # 👉️ 2023-09-27 00:00:00

The date string in the example is formatted as mm-dd-yyyy.

If you have a date string that is formatted in a different way, use this table of the docs to look up the format codes you should pass as the second argument to the strptime() method.

The second example adds days to the current date.

main.py
from datetime import datetime, timedelta current_date = datetime.today() print(current_date) # 👉️ 2022-06-20 09:42:22.341830 result_2 = current_date + timedelta(days=7) print(result_2) # 👉️ 2022-06-27 09:43:09.084770

The datetime.today() method returns the current local datetime.

The third example uses the date() class instead of the datetime class when adding days to a date.

main.py
from datetime import date, timedelta date_3 = date(2023, 9, 24) print(date_3) # 👉️ 2023-09-24 result_3 = date_3 + timedelta(days=3) print(result_3) # 👉️ 2023-09-27

The datetime.timedelta class can be passed the days we want to add to the the date or datetime objects.

The fourth example adds days to a date object that represents the current date.

main.py
from datetime import date, timedelta date_4 = date.today() print(date_4) # 👉️ 2022-06-20 result_4 = date_4 + timedelta(days=7) print(result_4) # 👉️ 2022-06-27

The date.today method returns a date object that represents the current local date.

Use the search field on my Home Page to filter through my more than 1,000 articles.