add minutes to datetime in python

How to Add Minutes to Datetime in Python

Python is a powerful programming language that allows you to perform wide range of tasks. Sometimes you may need to add minutes or time to datetime in Python. There are multiple ways to solve this problem. In this article, we will look at 3 different ways to add minutes to datetime in python.


How to Add Minutes to Datetime in Python

Here are the different ways to add time to date or datetime values in python – using timedelta, pandas and relativedelta.


1. Using datetime module

Python comes with datetime module that allows you to work with date, datetime, timedelta and time objects. For our example, we will add 15 minutes to our datetime value.

Here is how we will go about it. Let us say you have a datetime string to start with. First, we will convert it into a datetime object. Then we will add 15 minutes to this datetime object. Finally, we convert the datetime object back to string, which has the same format as the starting datetime string.

Here is the sample code for it.

from datetime import datetime
from datetime import timedelta

# Given timestamp in string
time_str = '23/9/2021 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)

In the above code, we first import datetime and timedelta classes. Then we define our starting time time_str and date format date_format_str. We obtain the datetime object given_time from string using strptime function. We obtain a timedelta object from 15 minutes, and add it to the datetime object, to obtain final datetime object.

We use strftime function to get final_time_str string from datetime object.


2. Using dateutil

dateutil is a library of functions and utilities that help you manipulate date, time and datetime values. It contains relativedelta class that represents datetime interval. Here too, we will obtain datetime object from datetime string, get relativedelta object from interval value, and then add it to datetime object to obtain final value.

Here is the above code using dateutil.

from datetime import datetime
from dateutil.relativedelta import relativedelta

# Given timestamp in string
time_str = '23/9/2021 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 30 minutes to datetime object
final_time = given_time + relativedelta(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)

Here also, we convert datetime string to datetime object using strptime function, use relativedelta function to obtain relativedelta object from time interval. We simply add this relativedelta object to datetime object to obtain final datetime object, which we convert to string using strftime function.


3. Using pandas

You can also use python pandas to add time interval to datetime. It provides DateOffset class which is similar to timedelta and relativedelta objects used above. It is used to increment or decrement given datetime value. Here is the code to add minutes to datetime value. Here also we convert datetime string to datetime object using strptime function, obtain dateoffset object from interval value, and add it to the datetime object to obtain final datetime.

from datetime import datetime
import pandas as pd

# Given timestamp in string
time_str = '23/9/2021 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 + pd.DateOffset(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)

In this article, we have looked at 3 different utilities to add time to datetime value. All of them use the same approach. First we convert a datetime string to datetime object using strptime function. Then we obtain an interval object (relativedelta/timedelta/dateoffset). We add this object to original datetime object, to obtain final datetime object. We use strftime function to convert datetime object to string.

Also read:

How to Disable iptables in Ubuntu
How to Create Swap Space in Ubuntu
How to Install RPM Package in Redhat, CentOS
How to Configure Firewalld in Redhat, CentOS in Linux
How to Restart Linux Server from Command Line

Leave a Reply

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