Python Subtract Minutes from DateTime Example Tutorial

Published On: 19/09/2022 | Category: Python


Hi Guys,

Today, I will let you know example of how to subtract minutes to date in python. I would like to show you how to subtract minute to current date in python. I would like to show you python minus minutes to date in dataframe. this example will help you python subtract minutes to datetime example.

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.

So let's see bellow example:

Example 1: Python Subtract Minutes to DateTime

main.py
from datetime import datetime
from dateutil.relativedelta import relativedelta
  
myDateString = "2022-09-19"
  
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-09-19 00:00:00
New Date :
2022-09-18 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-09-19 04:13:40.337710
New Date :
2022-09-19 03:53:40.337710

It will help you....

Happy Pythonic Coding!