Add weeks to a date in Python

avatar

Borislav Hadzhiev

Last updated: Jun 20, 2022

banner

Photo from Unsplash

Add weeks to a date in Python #

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

main.py
from datetime import datetime, date, timedelta # ✅ add weeks to a date my_str = '09-14-2023' # 👉️ (mm-dd-yyyy) date_1 = datetime.strptime(my_str, '%m-%d-%Y') print(date_1) # 👉️ 2023-09-14 00:00:00 result_1 = date_1 + timedelta(weeks=2) print(result_1) # 👉️ 2023-09-28 00:00:00 # ----------------------------------------------- # ✅ add weeks to current date current_date = datetime.today() print(current_date) # 👉️ 2022-06-20 14:11:16.561585 result_2 = current_date + timedelta(weeks=1) print(result_2) # 👉️ 2022-06-27 14:11:16.561585 # ----------------------------------------------- # ✅ using date instead of datetime date_3 = date(2023, 9, 7) print(date_3) # 👉️ 2023-09-07 result_3 = date_3 + timedelta(weeks=3) print(result_3) # 👉️ 2023-09-28 # ----------------------------------------------- # ✅ add weeks to current date (using date instead of datetime) date_4 = date.today() print(date_4) # 👉️ 2022-06-20 result_4 = date_4 + timedelta(weeks=2) print(result_4) # 👉️ 2022-07-04

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

In the examples we passed the weeks keyword argument to the timedelta class, but you can also pass days, e.g. timedelta(days=14).

Either way, 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 weeks to it.

main.py
from datetime import datetime, date, timedelta # ✅ add weeks to a date my_str = '09-14-2023' # 👉️ (mm-dd-yyyy) date_1 = datetime.strptime(my_str, '%m-%d-%Y') print(date_1) # 👉️ 2023-09-14 00:00:00 result_1 = date_1 + timedelta(weeks=2) print(result_1) # 👉️ 2023-09-28 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 weeks to the current date.

main.py
from datetime import datetime, timedelta current_date = datetime.today() print(current_date) # 👉️ 2022-06-20 14:11:16.561585 result_2 = current_date + timedelta(weeks=1) print(result_2) # 👉️ 2022-06-27 14:11:16.561585

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

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

main.py
from datetime import date, timedelta date_3 = date(2023, 9, 7) print(date_3) # 👉️ 2023-09-07 result_3 = date_3 + timedelta(weeks=3) print(result_3) # 👉️ 2023-09-28

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

The fourth example adds weeks 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(weeks=2) print(result_4) # 👉️ 2022-07-04

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

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.