In this article, we will be discussing three different ways to add minutes to a given timestamp in python. Using timedelta, pandas or relativedelta.

Suppose we have a timestamp as string format and we want to add N minutes to it. Here N can be any number. But the catch is that, we want the final timestamp in given string format only, after adding N minutes to it. Let’s see how to do that,

Add minutes to a timestamp in Python using timedelta

Python has a datetime module for the manipulation of date and time. It consists of following classes, date, time, datetime, timedelta, tzinfo. To add N minutes in a given timestamp, we are going to use two classes of the datetime module i.e. datetime and timedelta.

Steps to add N minutes to datetime are as follows,

  • Step 1: If the given timestamp is in string format, then convert it to the datetime object using datetime.strptime() function. Otherwise you can skip this step.
  • 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: Add the timedelta object to the datetime object created in step 1. It will give us a new datetime object, pointing to a new timestamp i.e. N minutes after the given timestamp.
  • Step 4: If you need the final timestamp in string, then use strftime() to convert the datetime object to string. For that, pass the format string as argument to strftime() and it will convert the datetime object to a string of the specified format.

Let’s understand with an example,

Add 15 minutes to a timestamp in python

from datetime import datetime
from datetime import timedelta

# Given timestamp in string
time_str = '23/2/2020 11:12:22.234513'
date_format_str = '%d/%m/%Y %H:%M:%S.%f'

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

print('Given timestamp: ', given_time)

n = 15
# Add 15 minutes to datetime object
final_time = given_time + timedelta(minutes=n)

print('Final Time (15 minutes after 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-02-23 11:12:22.234513
Final Time (15 minutes after given time ):  2020-02-23 11:27:22.234513
Final Time as string object:  23/02/2020 11:27:22.234513

We added 15 minutes to the timestamp ’23/2/2020 11:12:22.234513′ to make it ’23/02/2020 11:27:22.234513′.

It added a timedelta (of 15 minutes duration) to the datetime object, so it returned a new datetime object pointing to the new timestamp. Then it converted the final datetime object to the required string format using datetime.strftime(). If you want the final timestamp string in some other format, then you can change the format string while using strptime() & strftime().

Add minutes to given timestamp in Python using Pandas

Pandas provide a DateOffset class, to store the duration or interval information. It is mainly used in incrementing or decrementing a timestamp. It can be used with datetime module to add N minutes to a datetime. Let’s understand with an example,

Add 3 minutes to a datetime in python

from datetime import datetime
import pandas as pd

# Given timestamp in string
time_str = '23/2/2020 11:12:22.234513'
date_format_str = '%d/%m/%Y %H:%M:%S.%f'

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

print('Given timestamp: ', given_time)

n = 3
# Add 3 minutes to datetime object
final_time = given_time + pd.DateOffset(minutes=n)

print('Final Time (3 minutes after 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-02-23 11:12:22.234513
Final Time (3 minutes after given time ):  2020-02-23 11:15:22.234513 
Final Time as string object:  23/02/2020 11:15:22.234513

We created a DateOffset object by passing minutes argument as 3. Then added that to the datetime object. It returned a new datetime object pointing to a different timestamp i.e. after 3 minutes from the given timestamp.

Add minutes to given timestamp in python using relativedelta

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

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

Add 30 minutes to a timestamp in python

from datetime import datetime
from dateutil.relativedelta import relativedelta

# Given timestamp in string
time_str = '23/2/2020 11:12:22.234513'
date_format_str = '%d/%m/%Y %H:%M:%S.%f'

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

print('Given timestamp: ', given_time)

n = 30
# Add 30 minutes to datetime object
final_time = given_time + relativedelta(minutes=n)

print('Final Time (30 minutes after 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-02-23 11:12:22.234513
Final Time (30 minutes after given time ):  2020-02-23 11:42:22.234513
Final Time as string object:  23/02/2020 11:42:22.234513

As given timestamp was in string format, so we converted it to datetime. After that, we created a relativedelta object representing 30 minutes by passing minutes argument as 30. Then we added that to the datetime object. It returned a datetime object pointing to a new timestamp i.e. thirty minutes after the given timestamp.

Summary:

We learned about 3 different ways to add N minutes to a timestamp in python.