Subtract minutes from datetime in Python

In this article, we will discuss three different ways to subtract minutes from a given timestamp in python using timedelta, pandas or relativedelta.

Suppose you have a timestamp as string and you want to subtract N minutes from it, where N can be any number. But you want the final timestamp after subtracting N minutes, in the given string format. Let’s see how to do that,

Subtract minutes from a timestamp in Python using timedelta

Python provides a module datetime for manipulation of date and time. It consists of following classes – date, time, datetime, timedelta and tzinfo. To subtract minutes from a given timestamp, we are going to use the datetime and timedelta classes of the datetime module. The timedelta represents a duration, that represents the difference between two dates or times or datetimes.

Steps to subtract N minutes from datetime are as follows,

Step 1: If the given timestamp is in a string format, then we need to convert it to the datetime object.
Step 2: Create an object of timedelta, to represent an interval of N minutes. For that, pass the argument minutes with value N in the timedelta constructor.
Step 3: Subtract the timedelta object from the datetime object in step 1. It will give us a new datetime object, pointing to a new timestamp i.e. N minutes before the given timestamp.
Step 4: If you want the final timestamp in string format, then convert the datetime object to string using strftime(). You can pass the format string as argument and it will convert the datetime object to a string of the specified format.

Let’s understand with an example,

Subtract 2 minutes from a timestamp in python

from datetime import datetime
from datetime import timedelta

# Given timestamp in string
timestamp = '14/11/2020 21:21:58.432312'
date_format_str = '%d/%m/%Y %H:%M:%S.%f'

# create datetime object from timestamp string
given_time = datetime.strptime(timestamp, date_format_str)

print('Given Timestamp: ', given_time)

n = 2
# Subtract 2 minutes from datetime object
final_time = given_time - timedelta(minutes=n)

print('Final Time (2 minutes ahead of given time ): ', final_time)

# Convert datetime object to string in specific format 
final_time_str = final_time.strftime('%d/%m/%Y %H:%M:%S.%f')
print('Final Time as string object: ', final_time_str)

Output

Given Timestamp:  2020-11-14 21:21:58.432312
Final Time (2 minutes ahead of given time ):  2020-11-14 21:19:58.432312
Final Time as string object:  14/11/2020 21:19:58.432312

We subtracted 2 minutes from the timestamp ’14/11/2020 21:21:58.432312′ to make it ’14/11/2020 21:19:58.432312′.

Subtract minutes from given timestamp in Python using Pandas

Pandas provide a class DateOffset, to store the duration or interval information. It is mostly used to increment or decrement a timestamp. It can be used with datetime module to subtract N minutes from a datetime. Let’s understand with an example,

Subtract 3 minutes from a datetime in python

from datetime import datetime
import pandas as pd

# Given timestamp in string
timestamp = '14/11/2020 21:21:58.432312'
date_format_str = '%d/%m/%Y %H:%M:%S.%f'

# create datetime object from timestamp string
given_time = datetime.strptime(timestamp, date_format_str)

print('Given Time: ', given_time)

n = 3
# Subtract 3 minutes from datetime object
final_time = given_time - pd.DateOffset(minutes=n)

print('Final Time (3 minutes ahead of given time ): ', final_time)

# Convert datetime object to string in specific format 
final_time_str = final_time.strftime('%d/%m/%Y %H:%M:%S.%f')
print('Final Time as string object: ', final_time_str)

Output

Given Time:  2020-11-14 21:21:58.432312
Final Time (3 minutes ahead of given time ):  2020-11-14 21:18:58.432312 
Final Time as string object:  14/11/2020 21:18:58.432312

We created a DateOffset object by passing minutes argument as 3. Then we subtracted this DateOffset object to the datetime object. It returned a new datetime object pointing to a new timestamp i.e. 3 minutes ahead of the given timestamp.

Subtract minutes from given timestamp in python using relativedelta

In python, the dateutil module provides a class relativedelta, which represents an interval of time. The relativedelta class has all the attributes to represent a duration i.e. Year, Month, Day, Hours, Minutes, Seconds and Microseconds.

So, to subtract minutes from a given timestamp, we can create a relativedelta object to represents the interval in minutes and then subtract it from the datetime object containing give date. Let’s understand with an example,

Subtract 30 minutes from a timestamp in python

from datetime import datetime
from dateutil.relativedelta import relativedelta

# Given timestamp in string
timestamp = '14/11/2020 21:21:58.432312'
date_format_str = '%d/%m/%Y %H:%M:%S.%f'

# create datetime object from timestamp string
given_time = datetime.strptime(timestamp, date_format_str)

print('Given Time: ', given_time)

n = 30
# Subtract 30 minutes from datetime object
final_time = given_time - relativedelta(minutes=n)

print('Final Time (30 minutes ahead of given time ): ', final_time)

# Convert datetime object to string in specific format 
final_time_str = final_time.strftime('%d/%m/%Y %H:%M:%S.%f')
print('Final Time as string object: ', final_time_str)

Output

Given Time:  2020-11-14 21:21:58.432312
Final Time (30 minutes ahead of given time ):  2020-11-14 20:51:58.432312
Final Time as string object:  14/11/2020 20:51:58.432312

Summary:

We learned about three different ways to subtract N minutes from a timestamp in python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top