Javaexercise.com

Add Hours To Datetime In Python

Add hours to datetime in python

Python is a high level, interpreted programming language, designed and developed by Guido van Rossum in the 1990s. It is known for its simplicity and easy to understand capability. It 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 hours to a datetime in python. Let us consider an example where we have an arbitrary starting datetime, say 30th of March in the year 2022, 11 hours and we need to add 2 hours to this datetime. The resulting datetime should be 30th of March in the year 2022, 13 hours. Let us have a look at the different ways of doing this in detail.

1. Using the datetime and timedelta libraries

In this method, we use the datetime and timedelta libraries to add hours to a datetime. 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. 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 hours to this starting date using the timedelta() function, with the parameter hours 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 - 

Python 3 code

# Importing the required libraries

from datetime import datetime, timedelta

# Starting date

start_datetime = datetime(year = 2022, month = 3, day = 30, hour = 11)

# Performing the operation

new_datetime = start_datetime + timedelta(hours=2)

# Printing

print(new_datetime)

Output

2022-03-30 13:00:00

2. Using the pandas library

In this method, we use the pandas library to add hours to a datetime. To work with these libraries we must first import them using the import statement. We define a variable start_datetime that stores the starting date. The desired starting date in this example is 30th of March in the year 2022, 11 hours and we need to add 2 hours 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 hours to this starting datetime using the pandas.to_timedelta() function by passing the parameter as 2 and unit as h to specify that we are mentioning hours. 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 - 

Python 3 code

# Importing the required libraries

import pandas as pd

# Starting datetime

start_datetime = pd.to_datetime('30/3/2022 11:00:00')

# Performing the operation

new_datetime = start_datetime + pd.to_timedelta(2, unit = 'h')

# Printing

print(new_datetime)

Output

2022-03-30 13:00:00

3. Using the relativedelta library

In this method, we use the relativedelta library to add hours 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 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 hours to this starting date using the relativedelta() function by passing the parameter hours 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 - 

Python 3 code

# 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)

# Performing the operation

new_datetime = start_datetime + relativedelta(hours=2)

# Printing

print(new_datetime)

Output

2022-03-30 13:00:00

Conclusion

In this topic, we have learned the use and advantages of adding hours to a datetime 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.