Skip to main content

How to add Seconds to Datetime in Python

Manipulating dates and times often involves adding or subtracting time units.

This guide focuses specifically on adding minutes to datetime objects in Python, utilizing the timedelta class for accurate and straightforward calculations. We'll cover different scenarios, including working with date strings, current times, and extracting specific time components.

Adding Minutes 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 minutes to an existing datetime object, use the timedelta class with the minutes argument:

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(minutes=29)
print(new_datetime) # Output: 2023-11-24 09:59:00.000123
note

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

Creating a datetime Object Directly

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

from datetime import datetime, timedelta

datetime_obj = datetime(2023, 9, 24, 9, 30, 35)
new_datetime = datetime_obj + timedelta(minutes=15)
print(new_datetime) # Output: 2023-09-24 09:45:35

Adding Minutes to the Current Time

To add minutes to the current time, use datetime.today() to get a datetime object that stores the current date and time:

from datetime import datetime, timedelta

current_datetime = datetime.today()
future_datetime = current_datetime + timedelta(minutes=5)
print(future_datetime) # Output: (Varies depending on the current date and time, but the time component will be 5 minutes later)
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 hour, day, month, and year.

Extracting Time After Adding Minutes

If you only need the time component after the addition, use the .time() method on the resulting datetime object:

from datetime import datetime, timedelta

current_datetime = datetime.now()
future_datetime = current_datetime + timedelta(minutes=25)
future_time = future_datetime.time()
print(future_time) # Output (Varies based on the current time, but the time component will be 25 minutes later)

The datetime.time() method returns a time object representing the time part of the datetime object.

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(minutes=25)
formatted_time = f'{future_datetime:%H:%M:%S}'
print(formatted_time) # Output: (Time 25 minutes in the future, formatted as HH:MM:SS)

Adding Minutes to a time Object

If you have just a time object, you must combine it with a date object first using datetime.combine() to create a datetime object, so that you can use timedelta to add the minutes:

from datetime import datetime, date, timedelta, time

time_obj = time(9, 25)
combined_datetime = datetime.combine(date.today(), time_obj)
future_datetime = combined_datetime + timedelta(minutes=30)
future_time = future_datetime.time()
print(future_time) # Output: 09:55:00