Add & Subtract Weeks to & from Date in Python (2 Examples)
In this tutorial, I’ll explain how to add or remove weeks to/from a datetime object in the Python programming language.
The page looks as follows:
If you want to learn more about these contents, keep reading…
Example Data & Add-On Libraries
First, we have to load datetime and timedelta from the datetime module:
from datetime import datetime, timedelta # Import modules in Python
We use the following example data as a basement for this Python programming tutorial:
my_date = datetime.today() # Create Example date print(my_date) # Print actual date # 2022-06-10 15:40:02.298970
The Python console output shows the datetime of today.
Example 1: Add Specific Number of Weeks to Date
In this example, I’ll explain how to add a certain number of weeks to our example date.
For this, we can apply the timedelta function in combination with the weeks argument:
my_date_weeks_added = my_date + timedelta(weeks = 14) # Adding weeks using the timedelta function print(my_date_weeks_added) # Show date object with added weeks # 2022-09-16 16:38:23.520823
As demonstrated by the output above, 14 weeks have been added to our example datetime object.
Example 2: Subtract Specific Number of Weeks from Date
Of course, it’s also possible to remove weeks from a datetime object, this is what I’ll show you in the next example.
my_date_weeks_subtracted = my_date - timedelta(weeks = 14) # Subtracting days with the timedelta function print(my_date_weeks_subtracted) # Print date object with subtracted weeks # 2022-03-04 16:38:23.520823
Considering the Python code above, we simply have changed the operator before our timedelta function from ‘+’ to ‘-‘ and the certain number of weeks will be subtracted.
Video & Further Resources
Would you like to know more about adding & subtracting a specific number of weeks to or from a date? Then you might want to have a look at the following video on my YouTube channel. In the video, I’m explaining the topics of this tutorial.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might want to have a look at some related articles on my website:
- Get Current Day of Week in Python – Number & Name (3 Examples)
- Calculate Time Difference Between Two datetime Objects in Python (Example)
- Add Days, Months & Years to datetime Object in Python (3 Examples)
- All Python Programming Examples
To summarize: In this article, you have learned how to add & subtract weeks to/from a date in the Python programming language. Please tell me about it in the comments below, in case you have additional questions.
This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.