Last updated: Apr 8, 2024
Reading timeยท5 min

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.
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

Make sure to import the datetime or date and
timedelta
classes from the datetime module.
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.
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.
If you need to add days to the current date, use the datetime.today() method.
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

The
datetime.today()
method returns the current local datetime.
date class to add days to a dateYou can also use the date() class instead of the datetime class when adding
days to a date.
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

The
datetime.timedelta()
class can be passed the days we want to add to the date or datetime objects.
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.
date() method to get the date after the operationIf you only need to extract the date after the operation, call the date()
method on the datetime object.
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.
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
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.
date object that stores the current dateHere is an example of adding days to a date object that represents the current
date.
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

The
date.today()
method returns a date object that represents the current local date.
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.
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.
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.
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.
If you need to add weeks to the current date, use the datetime.today() method.
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.
date() class to add weeks to a dateYou can also use the date() class instead of the datetime class when adding
weeks to a date.
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.
date object that stores the current dateThe following example adds weeks to a date object that represents the current
date.
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.
You can learn more about the related topics by checking out the following tutorials: