How to Add Minutes to DateTime in Python?

By Hardik Savani October 30, 2023 Category : Python

I am going to explain you example of python add minutes to datetime example. This post will give you simple example of how to add minutes to date in python. I explained simply step by step how to add minute to current date in python. Here you will learn python add minutes to date in dataframe. You just need to some step to done python add 1 minute to date string.

In this example, I will give two examples for you of how to add minutes to date in python and how to add 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 Add 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")

addMinutesNumber = 20;
newDate = myDate + relativedelta(minutes=addMinutesNumber)

print("Old Date :")
print(myDate)

print("New Date :")
print(newDate)

Output:

Old Date :
2022-06-01 00:00:00
New Date :
2022-06-01 00:20:00

Example 2: Python Add Minutes to Current DateTime

main.py

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

Output:

Old Date :
2022-06-20 09:28:44.405902
New Date :
2022-06-20 09:48:44.405902

I hope it can help you...

Tags :
Shares