Python Add Hours to Date Example Tutorial
Published On: 08/09/2022 | Category:
Python

Hi Guys,
I am going to explain you example of python 3 add hours to date string. Here you will learn python add hours to date example. let’s discuss about how to add hours to date in python. I would like to show you python add hours to date in dataframe. So, let's follow few step to create example of python add 1 hour to date string.
In this example, I will give two examples for you of how to add hours to date in python and how to add hours to today's date in python. therefore, let's see below example code and try it.
So let's see bellow example:
Example 1: Python Add Hours to Date main.pyfrom datetime import datetime from dateutil.relativedelta import relativedelta myDateString = "2022-09-01" myDate = datetime.strptime(myDateString, "%Y-%m-%d") addHourNumber = 2; newDate = myDate + relativedelta(hours=addHourNumber) print("Old Date :") print(myDate) print("New Date :") print(newDate)Output:
Old Date : 2022-09-01 00:00:00 New Date : 2022-09-01 02:00:00Example 2: Python Add Hours to Current Date main.py
from datetime import datetime from dateutil.relativedelta import relativedelta myDate = datetime.today() addHourNumber = 2; newDate = myDate + relativedelta(hours=addHourNumber) print("Old Date :") print(myDate) print("New Date :") print(newDate)Output:
Old Date : 2022-09-08 04:49:08.621041 New Date : 2022-09-08 06:49:08.621041
It will help you....
Happy Coding!