Python Subtract Minutes from DateTime Example

By Hardik Savani October 30, 2023 Category : Python

Now, let's see article of python subtract minutes to datetime example. I’m going to show you about how to subtract minutes to date in python. it's simple example of how to subtract minute to current date in python. This post will give you simple example of python minus minutes to date in dataframe. Let's see bellow example python subtract 1 minute to date string.

In this example, I will give two examples for you of how to subtract minutes to date in python and how to subtract minutes to today's datetime in python. therefore, let's see below example code and try it.

You can use these examples with python3 (Python 3) version.

Example 1: Python Subtract Minutes to DateTime

main.py

from datetime import datetime
from dateutil.relativedelta import relativedelta
  
myDateString = "2022-06-01"
  
myDate = datetime.strptime(myDateString, "%Y-%m-%d")
  
subMinutesNumber = 20;
newDate = myDate - relativedelta(minutes=subMinutesNumber)
  
print("Old Date :")
print(myDate)
  
print("New Date :")
print(newDate)

Output:

Old Date :
2022-06-01 00:00:00
New Date :
2022-05-31 23:40:00

Example 2: Python Subtract Minutes to Current DateTime

main.py

from datetime import datetime
from dateutil.relativedelta import relativedelta
  
myDate = datetime.today()
  
subMinutesNumber = 20;
newDate = myDate - relativedelta(minutes=subMinutesNumber)
  
print("Old Date :")
print(myDate)
  
print("New Date :")
print(newDate)

Output:

	
Old Date :
2022-06-21 09:29:18.658343
New Date :
2022-06-21 09:09:18.658343

I hope it can help you...

Tags :