How To Subtract Minutes From Time In Python?

13-Jan-2022

.

Admin

How To Subtract Minutes From Time In Python?

Hello Friends,

Now let's see example of how to subtract minutes from time in python. I explained simply about how to get sub minutes from current datetime day in python. I am going to show you python datetime to sub minutes example. We will learn sub minutes in datetime using python.

In this post, You'll learn the datetime module is supplies classes for manipulating datetimes and times. This article will give you example of how to sub minutes to datetime in python.

I will show you for how to sub minutes to datetime in python. I am use datetime and time module to sub minutes datetime.

So let's start following example.

Example : 1


example1.py

# import datetime module

from datetime import datetime,timedelta

currentTimeDate = datetime.now() - relativedelta(minutes=5)

currentTime = currentTimeDate.strftime('%H:%M:%S')

print(currentTime)

Run Example

python example1.py

Output:

16:42:40

Example : 2

example2.py

# import datetime module

from datetime import datetime

from dateutil.relativedelta import relativedelta

currentTimeDate = datetime.now() - relativedelta(minutes=2)

currentTime = currentTimeDate.strftime('%H:%M:%S')

print(currentTime)

Run Example

python example2.py

Output:

16:41:25

Example : 3

example3.py

# import pandas module

import pandas as pd

initial_date = "2021-12-18 12:08:00"

req_date = pd.to_datetime(initial_date) - pd.DateOffset(minutes=3)

req_date = req_date.strftime('%H:%M:%S')

print(req_date)

Run Example

python example3.py

Output:

12:05:00

It will help you....

#Python