Add minutes to datetime in Python

avatar

Borislav Hadzhiev

Last updated: Jun 22, 2022

banner

Photo from Unsplash

Add minutes to datetime in Python #

Use the timedelta() class from the datetime module to add minutes to datetime, e.g. result = dt + timedelta(minutes=10). The timedelta class can be passed a minutes argument and adds the specified number of minutes to the datetime.

main.py
from datetime import datetime, timedelta # ✅ parse datetime string and add minutes to datetime d = '2023-11-24 09:30:00.000123' # 👇️ convert string to datetime object dt = datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f') print(dt) # 👉️ 2023-11-24 09:30:00.000123 result_1 = dt + timedelta(minutes=29) print(result_1) # 👉️ 2023-11-24 09:59:00.000123 # ----------------------- # ✅ add minutes to datetime dt_2 = datetime(2023, 9, 24, 9, 30, 35) print(dt_2) # 👉️ 2023-09-24 09:30:35 result_2 = dt_2 + timedelta(minutes=15) print(result_2) # 👉️ 2023-09-24 09:45:35 # ------------------------ # ✅ add minutes to current time now = datetime.today() print(now) # 👉️ 2022-06-22 12:35:30.302272 result_3 = now + timedelta(minutes=5) print(result_3) # 👉️ 2022-06-22 12:40:30.302272
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 creates a datetime object from a datetime string and adds minutes to it.

main.py
from datetime import datetime, timedelta # ✅ parse datetime string and add minutes to datetime d = '2023-11-24 09:30:00.000123' # 👇️ convert string to datetime object dt = datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f') print(dt) # 👉️ 2023-11-24 09:30:00.000123 result_1 = dt + timedelta(minutes=29) print(result_1) # 👉️ 2023-11-24 09:59:00.000123

The datetime.strptime() method returns a datetime object that corresponds to the provided date string, parsed according to the format.

If you have a date string that is formatted in a different way, use this table of the docs to look up the format codes you should pass as the second argument to the strptime() method.

The second example uses the datetime class to create a datetime object and the timedelta class to add minutes to it.

main.py
from datetime import datetime, timedelta dt_2 = datetime(2023, 9, 24, 9, 30, 35) print(dt_2) # 👉️ 2023-09-24 09:30:35 result_2 = dt_2 + timedelta(minutes=15) print(result_2) # 👉️ 2023-09-24 09:45:35

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

The third example adds minutes to the current time.

main.py
from datetime import datetime, timedelta now = datetime.today() print(now) # 👉️ 2022-06-22 12:35:30.302272 result_3 = now + timedelta(minutes=5) print(result_3) # 👉️ 2022-06-22 12:40:30.302272

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

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

This wouldn't be possible if we only had the time component. For example, 11:59:30PM + 15 minutes 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(9, 25) print(t) # 👉️ 09:25:00 result = datetime.combine(date.today(), t) + timedelta(minutes=30) print(result) # 👉️ 2022-06-22 09:55:00 only_t = result.time() print(only_t) # 👉️ 09:55:00

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 minutes 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(9, 25) print(t) # 👉️ 09:25:00 result = datetime.combine(date.today(), t) + timedelta(minutes=30) print(result) # 👉️ 2022-06-22 09:55:00 # ✅ only get updated time only_t = result.time() print(only_t) # 👉️ 09:55:00

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.