Skip to main content

How to add Hours to Datetime in Python

Working with dates and times often requires adding or subtracting time units.

This guide specifically focuses on adding hours to datetime objects in Python, leveraging the timedelta class for accurate and straightforward calculations.

Adding Hours to a datetime Object

The datetime module provides the timedelta class, which is used to represent a duration or difference between two dates or times. To add hours to an existing datetime object:

from datetime import datetime, timedelta

date_string = '2023-11-24 09:30:00.000123'
datetime_obj = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f') #Create datetime from string

new_datetime = datetime_obj + timedelta(hours=3)
print(new_datetime) # Output: 2023-11-24 12:30:00.000123
note

The strptime() method parses a date string according to the format provided as the second argument. The timedelta(hours=N) creates a duration object, where N is the number of hours to be added. The + operator is overloaded for direct addition of timedelta objects to datetime objects.

Creating a datetime Object Directly

You can create a datetime object directly by specifying year, month, day, hour, minute, and second values:

from datetime import datetime, timedelta

datetime_obj = datetime(2023, 9, 24, 9, 30, 35)
new_datetime = datetime_obj + timedelta(hours=4)
print(new_datetime) # Output: 2023-09-24 13:30:35

Adding Hours to the Current Time

To work with the current time, use datetime.today() to get a datetime object containing the current date and time, then add hours using timedelta:

from datetime import datetime, timedelta

current_datetime = datetime.today()
future_datetime = current_datetime + timedelta(hours=5)
print(future_datetime) # Output: (Current datetime + 5 hours, varies based on when you run the code)
note

datetime.today() returns a datetime object representing the current local date and time. Using a datetime object is important to handle possible rollovers to next day, month, and year.

Extracting Time After Adding Hours

If you only need the updated time after adding hours, use the .time() method on the resulting datetime object:

from datetime import datetime, timedelta

current_datetime = datetime.now()
future_datetime = current_datetime + timedelta(hours=5)
future_time = future_datetime.time()
print(future_time) # Output: (Time 5 hours in the future, e.g., 10:41:07.814924)

Formatting Time as HH:MM:SS

To format the time as HH:MM:SS, use f-strings with format specifiers:

from datetime import datetime, timedelta

current_datetime = datetime.now()
future_datetime = current_datetime + timedelta(hours=3)
formatted_time = f'{future_datetime:%H:%M:%S}'
print(formatted_time) # Output: (Time 3 hours in the future formatted as HH:MM:SS e.g. 08:41:40)

Adding Hours to a Time Object

If you have just a time object, you must combine it with a date object to create a datetime object before adding hours using timedelta. The datetime.combine() method is used to combine a date and a time object into a single datetime object.

from datetime import datetime, date, timedelta, time

time_obj = time(6, 25)
combined_datetime = datetime.combine(date.today(), time_obj)
future_datetime = combined_datetime + timedelta(hours=6)
future_time = future_datetime.time()
print(future_time) # Output: 12:25:00
note

If you need to add hours to a specific time, you must use the datetime.combine() method to create a datetime object by combining your time object with a date object first, before you can add hours.