Mathematical Operations on Dates in Python (Example)
In this tutorial you’ll learn how to do mathematical operations with dates using the Python programming language.
The table of content is structured as follows:
Let’s dive into it!
Importing Modules and Example Data Construction
from datetime import datetime, date, timedelta from dateutil.relativedelta import relativedelta
Afterwards, we initialize a date, a datetime object and a day amount which is simply an integer to use in our examples.
day_amount = 6500 start_date = date(1990,1,1) start_datetime = datetime(1990,1,1)
Example 1: Addition With Dates
The timedelta() function can be utilized with the days parameter to add a desired number of days to a certain date.
print(start_date + timedelta(days = day_amount)) print(start_datetime + timedelta(days = day_amount)) # 2007-10-19 # 2007-10-19 00:00:00
This method can be utilized to go to the future relative to a given date.
Based on the previous output, you can also see the difference between date and datetime objects: datetime objects do also contain 00:00:00 (i.e. hours, minutes, and seconds).
Example 2: Subtraction With Dates
The relativedelta() function can be used interchangeably with the timedelta() function.
print(start_datetime - relativedelta(days = day_amount)) # 1972-03-16 00:00:00
Similar to the previous example, this method can be utilized to go to the past relative to a given date.
Example 3: Using Various Time Units
If you have the time amount for your operations in various time units, then you can follow the method below.
year_amount = 5 month_amount = 8 day_amount = 24
The order of the parameters is not relevant for the structure above, but if you are going to use the functions without declaring which entry is for which parameter like
years=1999 then you have to specify the order of entries carefully.
So, here the order for the entries is important:
start_datetime = datetime(1999, 9, 14, 23, 10, 5) print(start_datetime) # 1999-09-14 23:10:05
However, with the declaration of the parameters, the order is irrelevant.
end_datetime = start_datetime + relativedelta( months = month_amount, years = year_amount, days = day_amount) print(end_datetime) # 2005-06-07 23:10:05
Video, Further Resources & Summary
Do you need more explanations on how to apply mathematical operations with dates and times? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- Get Time Zone of Own System in Python
- Calculate Time Difference Between Two Columns of pandas DataFrame in Python
- Calculate Number of Hours, Minutes & Seconds Between Two datetimes in Python
- Calculate Number of Years, Months & Days Between Two Dates in Python
- Calculate Time Difference Between Two datetime Objects in Python
- Calculate Time Difference in Milliseconds Between Two datetimes
- How to Add & Subtract Weeks to & from Date in Python
- Add Days, Months & Years to datetime Object
This post has shown how to do mathematical operations with dates. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.