How to add Seconds to Datetime in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Add seconds to datetime in Python

Use the timedelta() class from the datetime module to add seconds to datetime.

The timedelta class can be passed a seconds argument and adds the specified number of seconds to the datetime object.

main.py
from datetime import datetime, timedelta # โœ… Add seconds to a datetime object dt = datetime(2023, 9, 24, 9, 30, 35) print(dt) # ๐Ÿ‘‰๏ธ 2023-09-24 09:30:35 result = dt + timedelta(seconds=24) print(result) # ๐Ÿ‘‰๏ธ 2023-09-24 09:30:59 # ------------------------------ # โœ… Add seconds to the current time now = datetime.today() print(now) # ๐Ÿ‘‰๏ธ 2023-07-20 17:40:43.310804 result = now + timedelta(seconds=15) print(result) # ๐Ÿ‘‰๏ธ 2023-07-20 17:40:58.310804

add seconds to datetime

The code for this article is available on GitHub
If you only have a time component, e.g. 09:30:13 scroll down to the code sample that uses datetime.combine.

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 seconds to it.

main.py
from datetime import datetime, timedelta # โœ… Add seconds to datetime dt = datetime(2023, 9, 24, 9, 30, 35) print(dt) # ๐Ÿ‘‰๏ธ 2023-09-24 09:30:35 result = dt + timedelta(seconds=24) print(result) # ๐Ÿ‘‰๏ธ 2023-09-24 09:30:59

The code sample adds 24 seconds to the datetime object.

# Adding seconds to the current time

If you need to add seconds to the current time, use the datetime.today() method to get a datetime object that stores the current date and time.

main.py
from datetime import datetime, timedelta now = datetime.today() print(now) # ๐Ÿ‘‰๏ธ 2023-07-20 17:42:19.347301 result = now + timedelta(seconds=15) print(result) # ๐Ÿ‘‰๏ธ 2023-07-20 17:42:34.347301

add seconds to current time

The code for this article is available on GitHub

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 + 50 seconds would raise an exception.

# Add seconds to a datetime object using datetime.combine

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, 30, 13) print(t) # ๐Ÿ‘‰๏ธ 09:30:13 result = datetime.combine(date.today(), t) + timedelta(seconds=27) print(result) # ๐Ÿ‘‰๏ธ 2023-07-20 09:30:40 only_t = result.time() print(only_t) # ๐Ÿ‘‰๏ธ 09:30:40

add seconds to datetime using datetime combine

The code for this article is available on GitHub

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 seconds to it.

# Extracting the time after the operation

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, 30, 13) print(t) # ๐Ÿ‘‰๏ธ 09:30:13 result = datetime.combine(date.today(), t) + timedelta(seconds=27) print(result) # ๐Ÿ‘‰๏ธ 2023-07-20 09:30:40 # โœ… Only get the updated time only_t = result.time() print(only_t) # ๐Ÿ‘‰๏ธ 09:30:40

extracting time after the operation

The code for this article is available on GitHub

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

# Formatting the time as HH:MM:SS

If you need to get the time formatted as HH:MM:SS, use a formatted string literal.

main.py
from datetime import datetime, timedelta now = datetime.now() print(now) # ๐Ÿ‘‰๏ธ 2023-07-20 17:47:28.100856 result = now + timedelta(seconds=10) print(result) # ๐Ÿ‘‰๏ธ 2023-07-20 17:47:38.100856 print(result.time()) # ๐Ÿ‘‰๏ธ 17:47:38.100856 print(f'{result:%H:%M:%S}') # ๐Ÿ‘‰๏ธ 17:47:38

format time as hh mm ss

The code for this article is available on GitHub

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format specification mini-language in expression blocks.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev