How to perform arithmetic operations on a date in Python?


In this article, we will show you how to perform arithmetic operations on a date in Python. Now we see 5 examples for this task−

  • Adding days to a given Date

  • Subtracting days to a given Date

  • Adding Days and Hours to the given date

  • Subtracting months from a current date

  • Adding years for a given date

Example 1: Adding days to a given Date

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the datetime module.

  • Enter the date and create a variable to store it.

  • Enter the date format string and create a variable to store it.

  • Format the given date using the strptime() function (formats a time stamp in string format into a date−time object) and add 1 day to it using the timedelta() function (It is typically used to calculate differences between dates. It can also be used to manipulate dates in Python, and this function makes it very simple for users to do so).

  • Format the new date again using the strftime() function (returns a string that represents a datetime object based on the format codes) by passing the new date object and date format as an arguments to it.

  • Print the new Date after incrementing the old date with 1.

The following program returns the given date incremented by 1−

# importing datetime module import datetime # input date inputDate = '21-04-2013' # giving the date format date_format = '%d-%m-%Y' # formatting the date using strptime() function and adding 1 day to it date = datetime.datetime.strptime(inputDate, date_format) + datetime.timedelta(days=1) # Formatting the date date=date.strftime(date_format) # Printing the modified date print('New Date after incrementing old date {',inputDate,'} by 1 is:',date)

Output

New Date after incrementing old date { 21-04-2013 } by 1 is: 22-04-2013

Example 2: Subtracting days to a given Date

It is similar to the previous method, but this time we subtract the given date by some random number, say 2, as shown below.

The following program returns the given date decremented by 2−

import datetime # input date inputDate = '21-04-2013' # giving the date format date_format = '%d-%m-%Y' # formatting the date using strptime() function and subtracting 2 days from it date = datetime.datetime.strptime(inputDate, date_format) - datetime.timedelta(days=2) # Formatting the date date=date.strftime(date_format) # Printing the modified date print('New Date after decrementing old date {',inputDate,'} by 2 is:',date)

Output

New Date after decrementing old date { 21-04-2013 } by 2 is: 19-04-2013

Example 3: Adding Days and Hours to the given date

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword for importing the datetime, timedelta modules

  • Enter the current date and time using the datetime.now() function (returns the current local date and time) to get the current date and time in HH:MM:SS format.

  • Increment the current date with timedelta() function by passing the days and hours as an argument to it and store this new date in a variable.

  • Print the new Date after incrementing the old date with the given days and hours.

The following program returns the given date incremented by hours and days−

# importing datetime, timedelta modules from datetime import datetime, timedelta # getting the current date and time currentDate = datetime.now() print("The Current Date and Time = ", currentDate) # Incrementing the current date with some random days and hours newDate = currentDate + timedelta(days=4, hours=3) print('New Date:',newDate)

Output

The Current Date and Time = 2022-09-07 02:26:21.098855
New Date: 2022-09-11 05:26:21.098855

Example 4: Subtracting months from a current date

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword for importing the datetime, relativedelta modules

  • Enter the input date and time using the datetime() function.

  • Print the given input date and time.

  • Subtract n months(say 5) from the given date by passing the months as an argument to the relativedelta() function (The relativedelta type is intended to be applied to an existing datetime and can be used to replace specific components of that datetime or to represent a time interval).

  • Print the new Date after subtracting months from it.

The following program returns the given date subtracted by months−

# importing datetime, relativedelta modules import datetime from dateutil.relativedelta import relativedelta # input date and time in (yy,mm,dd,hh,mm,ss) inputDatetime = datetime.datetime(2019, 9, 12, 8, 40, 7) # printing the input date and time print("Input Date and time = ", inputDatetime) # Subtracting 5 months using the relativedelta function newDate = inputDatetime - relativedelta(months = 5) # Printing the modified date print("5 Months Ago = ", newDate)

Output

Input Date and time = 2019-09-12 08:40:07
5 Months Ago = 2019-04-12 08:40:07

Example 5: Adding years for a given date

The method is the same as the one above, but in this case we use the relativedelta() function to increment the given date from some arbitrary years, like 8

The following program returns the given date incremented by years−

# importing datetime, relativedelta modules import datetime from dateutil.relativedelta import relativedelta # input date and time in (yy,mm,dd,hh,mm,ss) inputDatetime = datetime.datetime(2019, 9, 12, 8, 40, 7) # printing the input date and time print("Input Date and time = ", inputDatetime) # Incrementing 8 years using the relativedelta function newDate = inputDatetime + relativedelta(years = 8) # Printing the modified date print("After 8 years = ", newDate)

Output

Input Date and time = 2019-09-12 08:40:07
After 8 years = 2027-09-12 08:40:07

Conclusion

In this article, we learned how to use five different examples to apply arithmetic operations for the given date, such as increasing and decreasing the years, months, days, and hours.

Updated on: 02-Nov-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements