Skip to main content

How to Add Days and Weeks to Dates in Python

Manipulating dates often involves adding or subtracting specific time units.

This guide focuses on adding days and weeks to dates and datetimes in Python, using the timedelta class for accurate calculations. We'll cover various scenarios, including working with date strings, current dates, and extracting only the date component.

Adding Days to a Date

Use the timedelta class from the datetime module to add days to a datetime object.

from datetime import datetime, timedelta

my_str = '09-24-2023'
date_1 = datetime.strptime(my_str, '%m-%d-%Y')

result = date_1 + timedelta(days=3)
print(result) # Output: 2023-09-27 00:00:00
  • The timedelta(days=3) creates a duration representing 3 days.
  • The date and time are updated as needed when you add the timedelta to the datetime object.

You can use this technique with dates that are formatted differently, for example, using a different format code string for strptime:

from datetime import datetime, timedelta

my_str = '2023-09-24 00:00:00'
date_1 = datetime.strptime(my_str, '%Y-%m-%d %H:%M:%S')

result = date_1 + timedelta(days=3)
print(result) # Output: 2023-09-27 00:00:00

Adding Days to the Current Date

To add days to the current date, use datetime.today() to get the current date and time:

from datetime import datetime, timedelta

current_date = datetime.today()
result = current_date + timedelta(days=7)
print(result) # Output (will depend on the current date, but the date component will be seven days later)
  • The datetime.today() method returns the current local date and time.
  • The timedelta object is then added to the datetime object, and the result is stored in the variable named result.

Adding Days Using the date Class

You can use the date class instead of datetime if you only need to manipulate the date component:

from datetime import date, timedelta

date_1 = date(2023, 9, 24)
result = date_1 + timedelta(days=3)
print(result) # Output: 2023-09-27

Extracting the Date After Adding Days

If you need to extract the date part after the operation, use the .date() method:

from datetime import datetime, timedelta

now = datetime.now()
result = now + timedelta(days=5)
print(result.date()) # Output: (Date five days in the future)
  • The method date() extracts the date component from the datetime object.

Formatting the Date After Adding Days

Use f-strings with format specifiers to display dates in the desired format:

from datetime import datetime, timedelta

now = datetime.now()
result = now + timedelta(days=6)
print(f'{result:%Y-%m-%d %H:%M:%S}') # Output (The datetime six days in the future, formatted as YYYY-MM-DD HH:MM:SS)

Adding Weeks to a Date

Use the timedelta class to add weeks to a datetime object by passing in a value to the weeks argument:

from datetime import datetime, timedelta
my_str = '09-14-2023'
date_1 = datetime.strptime(my_str, '%m-%d-%Y')

result = date_1 + timedelta(weeks=2)
print(result) # Output: 2023-09-28 00:00:00
  • The timedelta(weeks=2) creates a duration of 2 weeks.
  • This is then added to the datetime object.

Adding Weeks to Current Date

To add weeks to the current date use the datetime.today() function and then add a timedelta object using the weeks parameter:

from datetime import datetime, timedelta

current_date = datetime.today()
result = current_date + timedelta(weeks=1)
print(result) # Output (Will depend on current date, but the result will be one week in the future)

Adding Weeks Using the date Class

If you only need to work with dates, you can use the date class instead of datetime:

from datetime import date, timedelta

date_1 = date(2023, 9, 7)
result = date_1 + timedelta(weeks=3)
print(result) # Output: 2023-09-28
  • This will create a date three weeks in the future of 2023-09-07.

To work with the current date as a date object:

from datetime import date, timedelta

date_1 = date.today()
result = date_1 + timedelta(weeks=2)
print(result)