Add time to a datetime object in Python

avatar

Borislav Hadzhiev

Last updated: Jun 22, 2022

banner

Photo from Unsplash

Add time to a datetime object in Python #

Use the timedelta() class from the datetime module to add time to datetime, e.g. result = dt + timedelta(hours=2, minutes=25, seconds=24). The timedelta class can be passed the hours, minutes and seconds arguments and adds the specified time to the datetime.

main.py
from datetime import datetime, timedelta # ✅ add time to datetime dt = datetime(2023, 9, 24, 9, 30, 35) print(dt) # 👉️ 2023-09-24 09:30:35 result = dt + timedelta(hours=2, minutes=25, seconds=24) print(result) # 👉️ 2023-09-24 11:55:59 print(result.time()) # 👉️ 11:55:59 # ------------------------------ # ✅ add time to current time now = datetime.today() print(now) # 👉️ 2022-06-22 15:45:59.732804 result_2 = now + timedelta(hours=15) print(result_2) # 👉️ 2022-06-23 06:45:59.732804 print(result_2.time()) # 👉️ 06:45:59.732804
If you only have a time component, e.g. 09:30:13 scroll down to the last code snippet.

Make sure to import the datetime and timedelta classes from the datetime module.

The first example uses the datetime class to create a datetime object.

We passed values for the year, month, day, hour, minute and second arguments.

Once we have a datetime object, we can use the timedelta class to add time to it.

main.py
from datetime import datetime, timedelta # ✅ add time to datetime dt = datetime(2023, 9, 24, 9, 30, 35) print(dt) # 👉️ 2023-09-24 09:30:35 result = dt + timedelta(hours=2, minutes=25, seconds=24) print(result) # 👉️ 2023-09-24 11:55:59 print(result.time()) # 👉️ 11:55:59

The timedelta class can be passed the days, weeks, hours, minutes, seconds, milliseconds and microseconds as arguments.

All of the arguments are optional and default to 0.

The second example adds time to the current time.

main.py
from datetime import datetime, timedelta now = datetime.today() print(now) # 👉️ 2022-06-22 15:45:59.732804 result_2 = now + timedelta(hours=15) print(result_2) # 👉️ 2022-06-23 06:45:59.732804 print(result_2.time()) # 👉️ 06:45:59.732804

The datetime.today() method returns the current local datetime.

We need to use a datetime object because it automatically rolls over the minutes, hours, days, months and years if necessary.

This wouldn't be possible if we only had the time component. For example, 11:59:30PM + 2 hours would raise an exception.

If you only have the time component, use the datetime.combine method to combine the time with the current (or some other) date and get a datetime object.
main.py
from datetime import datetime, date, timedelta, time t = time(8, 25, 15) print(t) # 👉️ 08:25:15 result = datetime.combine(date.today(), t) + \ timedelta(hours=4, minutes=10, seconds=10) print(result) # 👉️ 2022-06-22 12:35:25 only_t = result.time() print(only_t) # 👉️ 12:35:25

The datetime.combine method takes a date and time as arguments and returns a new datetime object by combining them.

Once we get a datetime object, we can use the timedelta class to add time to it.

Use the time() method on the datetime object if you only need to extract the time after the operation.

main.py
from datetime import datetime, date, timedelta, time t = time(8, 25, 15) print(t) # 👉️ 08:25:15 result = datetime.combine(date.today(), t) + \ timedelta(hours=4, minutes=10, seconds=10) print(result) # 👉️ 2022-06-22 12:35:25 # ✅ only extract the time component only_t = result.time() print(only_t) # 👉️ 12:35:25

The datetime.time method returns a time object with the same hour, minute, second and millisecond.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.