How to add Days or Weeks to a Date in Python

avatar
Borislav Hadzhiev

Last updated: Feb 18, 2023
5 min

banner

# Table of Contents

  1. Add days to a date in Python
  2. Add weeks to a date in Python

# Add days to a date in Python

Use the timedelta() class from the datetime module to add days to a date.

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 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 # โœ… Add 3 days to a date result = date_1 + timedelta(days=3) print(result) # ๐Ÿ‘‰๏ธ 2023-09-27 00:00:00

add days to date

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 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 = date_1 + timedelta(days=3) print(result) # ๐Ÿ‘‰๏ธ 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.

# Adding days to the current date

If you need to add days to the current date, use the datetime.today() method.

main.py
from datetime import datetime, timedelta current_date = datetime.today() print(current_date) # ๐Ÿ‘‰๏ธ 2023-07-22 10:43:15.536277 # โœ… Add 7 days to the current date result = current_date + timedelta(days=7) print(result) # ๐Ÿ‘‰๏ธ 2023-07-29 10:43:15.536277

adding days to current date

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

# Using the date class to add days to a date

You can also use the date() class instead of the datetime class when adding days to a date.

main.py
from datetime import date, timedelta date_1 = date(2023, 9, 24) print(date_1) # ๐Ÿ‘‰๏ธ 2023-09-24 reuslt = date_1 + timedelta(days=3) print(reuslt) # ๐Ÿ‘‰๏ธ 2023-09-27

using date class to add days to date

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

The timedelta class can be passed the days, weeks, hours, minutes,seconds, milliseconds and microseconds as arguments.

All of the arguments are optional and default to 0.

It's best to only use keyword arguments in a call to the timedelta class as the order of arguments can be confusing.

# Using the date() method to get the date after the operation

If you only need to extract the date after the operation, call the date() method on the datetime object.

main.py
from datetime import datetime, timedelta now = datetime.now() print(now) # ๐Ÿ‘‰๏ธ 2023-02-18 18:41:00.827570 result = now + timedelta(days=5) print(result) # ๐Ÿ‘‰๏ธ 2023-02-23 18:41:00.827570 print(result.date()) # ๐Ÿ‘‰๏ธ 2023-02-23

The datetime.date method returns a date object with the same year, month and day.

If you need to format the date in a certain way, use a formatted string literal.

main.py
from datetime import datetime, timedelta now = datetime.now() print(now) # ๐Ÿ‘‰๏ธ 2023-02-18 18:41:30.558332 result = now + timedelta(days=6) print(result) # ๐Ÿ‘‰๏ธ 2023-02-24 18:41:30.558332 print(result.date()) # ๐Ÿ‘‰๏ธ 2023-02-24 print(f'{result:%Y-%m-%d %H:%M:%S}') # ๐Ÿ‘‰๏ธ 2023-02-24 18:41:30
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format specification mini-language in expression blocks.

# Adding days to a date object that stores the current date

Here is an example of adding days to a date object that represents the current date.

main.py
from datetime import date, timedelta date_1 = date.today() print(date_1) # ๐Ÿ‘‰๏ธ 2023-07-22 result = date_1 + timedelta(days=7) print(result) # ๐Ÿ‘‰๏ธ 2023-07-29

adding days to date object that stores current date

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

# Add weeks to a date in Python

Use the timedelta() class from the datetime module to add weeks to a date.

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 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 # โœ… add 2 weeks to a date result = date_1 + timedelta(weeks=2) print(result) # ๐Ÿ‘‰๏ธ 2023-09-28 00:00:00

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

We passed the weeks keyword argument to the timedelta class, but you can also pass it a days argument, e.g. timedelta(days=14).

Either way, the month (and year) will roll over if necessary.

The 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 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 = date_1 + timedelta(weeks=2) print(result) # ๐Ÿ‘‰๏ธ 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.

# Adding weeks to the current date

If you need to add weeks to the current date, use the datetime.today() method.

main.py
from datetime import datetime, timedelta current_date = datetime.today() print(current_date) # ๐Ÿ‘‰๏ธ 2023-02-18 18:46:07.541963 result = current_date + timedelta(weeks=1) print(result) # ๐Ÿ‘‰๏ธ 2023-02-25 18:46:07.541963

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

# Using the date() class to add weeks to a date

You can also use the date() class instead of the datetime class when adding weeks to a date.

main.py
from datetime import date, timedelta date_1 = date(2023, 9, 7) print(date_1) # ๐Ÿ‘‰๏ธ 2023-09-07 result = date_1 + timedelta(weeks=3) print(result) # ๐Ÿ‘‰๏ธ 2023-09-28

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

# Adding weeks to a date object that stores the current date

The following example adds weeks to a date object that represents the current date.

main.py
from datetime import date, timedelta date_1 = date.today() print(date_1) # ๐Ÿ‘‰๏ธ 2023-02-18 result = date_1 + timedelta(weeks=2) print(result) # ๐Ÿ‘‰๏ธ 2023-03-04

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

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev