How To Add Hours To The Current Time In Python?

Jan 06, 2022

.

Admin

How To Add Hours To The Current Time In Python?

Hello Friends,

This article will give you example of how to add hours from current datetime day in python. i explained simply about add hours to current datetime python. This tutorial will give you simple example add hours in datetime using python.

In this post, i will show you add hours to datetime in python. we need to add so you add hours to datetime python pandas.

The datetime module is supplies classes for manipulating datetimes and times. This article will give you example of how to add hours to datetime in python.

So let's start following example.

Example : 1


example1.py

# import datetime module

from datetime import datetime,timedelta

currentTimeDate = date.today() + relativedelta(hours=5)

currentTime = currentTimeDate.strftime('%H:%M:%S')

print(currentTime)

Run Example

python example1.py

Output:

05:00:00

Example : 2

example2.py

# import datetime module

from datetime import datetime

from dateutil.relativedelta import relativedelta

currentTimeDate = datetime.now() + relativedelta(hours=2)

currentTime = currentTimeDate.strftime('%H:%M:%S')

print(currentTime)

Run Example

python example2.py

Output:

19:52:47

Example : 3

example3.py

# import pandas module

import pandas as pd

initial_date = "2021-12-18 12:08:00"

req_date = pd.to_datetime(initial_date) + pd.DateOffset(hours=3)

req_date = req_date.strftime('%H:%M:%S')

print(req_date)

Run Example

python example3.py

Output:

15:08:00

It will help you....

#Python