Python is a high-level, interpreted programming language that is designed and developed by Guido van Rossum in the 1990s. It is known for its simplicity and easy-to-understand capability and is one of the most popular programming languages present in the software industry.
One of the most important concepts in python is that of adding minutes to a date-time in python. Let us consider an example where we have an arbitrary starting date-time, say 30th of March in the year 2022, 11 hours and we need to add 2 minutes to this date-time. The resulting date-time should be the 30th of March in the year 2022, 11 hours and 2 minutes. Let us have a look at the different ways of doing this in detail.
In this method, we use the datetime and timedelta libraries to add minutes to a date-time object.
To work with these libraries we must first import them using the import statement.
Here, we define a variable start_datetime that stores the starting datetime. The desired starting datetime in this example is the 30th of March in the year 2022, 11 hours.
This is specified using the datetime() function with the parameters year, month, day, and hours passed as 2022, 3, 30, and 11 respectively.
Next, we add 2 minutes to this starting date using the timedelta() function, with the parameter minutes passed as 2, and the addition operator +.
The result is stored in the variable new_datetime and later printed. Let us look at the Python 3 code and corresponding output for this method:
# Importing the required libraries
from datetime import datetime, timedelta
# Starting date
start_datetime = datetime(year = 2022, month = 3, day = 30, hour = 11)
print(start_datetime)
# Performing the operation
new_datetime = start_datetime + timedelta(minutes=2)
# Printing
print("Date time after adding minutes: ")
print(new_datetime)
Output
2022-03-30 11:00:00
2022-03-30 11:02:00
In this method, we use the pandas library to add minutes to a date-time.
To work with these libraries we must first import them using the import statement. We define a variable start_datetime that stores the starting datetime.
The desired starting datetime in this example is 30th of March in the year 2022, 11 hours and we need to add 2 minutes to this. This is specified using the pandas.to_datetime() function with the parameter passed as the date in string format in the form 30/3/2022 11:00:00, i.e. day month year hours minutes seconds format.
Next, we add 2 minutes to this starting datetime using the pandas.to_timedelta() function by passing the parameter as 2 and unit as m to specify that we are mentioning minutes. The result is stored in the variable new_datetime which is later printed.
Let us look at the Python 3 code and corresponding output for this method:
# Importing the required libraries
import pandas as pd
# Starting datetime
start_datetime = pd.to_datetime('30/3/2022 11:00:00')
print(start_datetime)
# Performing the operation
new_datetime = start_datetime + pd.to_timedelta(2, unit = 'm')
# Printing
print("Date time After adding minutes: ")
print(new_datetime)
Output
2022-03-30 11:00:00
2022-03-30 11:02:00
In this method, we use the relativedelta library to add minutes to a datetime object.
To work with these libraries we must first import them using the import statement. We define a variable start_datetime that stores the starting datetime. The desired starting datetime in this example is the 30th of March in the year 2022, 11 hours. This is specified using the datetime() function with the parameters year, month, day, and hours passed as 2022, 3, 30, and 11 respectively.
Next, we add 2 minutes to this starting date using the relativedelta() function by passing the parameter minutes as 2. The result is stored in the variable new_datetime which is later printed.
Let us look at the Python 3 code and corresponding output for this method:
# Importing the required libraries
from datetime import datetime
from dateutil.relativedelta import relativedelta
# Starting datetime
start_datetime = datetime(year = 2022, month = 3, day = 30, hour = 11)
print(start_datetime)
# Performing the operation
new_datetime = start_datetime + relativedelta(minutes=2)
# Printing
print("Date time After adding minutes: ")
print(new_datetime)
Output
2022-03-30 11:00:00
2022-03-30 11:02:00
You can also add minutes to the current date-time as well. First, you need to use the now() function to get the current date-time and then used the relativedelta() function to add the specified numbers of minutes. See the below Python code.
# Importing the required libraries
from datetime import datetime
from dateutil.relativedelta import relativedelta
# current datetime
start_datetime = datetime.now()
print(start_datetime)
# Performing the operation
new_datetime = start_datetime + relativedelta(minutes=2)
# Printing
print("Date time After adding minutes: ")
print(new_datetime)
Output:
2022-07-21 15:30:57.579896
Date time After adding minutes:
2022-07-21 15:32:57.579896
In this topic, we have learned the use and advantages of adding minutes to a date-time in a Python program, following some simple running examples of programs, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.