Explain Codes LogoExplain Codes Logo

What is the standard way to add N seconds to datetime.time in Python?

python
timedelta
datetime
dateutil
Anton ShumikhinbyAnton Shumikhin·Mar 12, 2025
TLDR

Inject N seconds directly into your datetime.time object by converting it to a datetime.datetime object, pulling the lever with timedelta, and then converting it back to datetime.time:

from datetime import datetime, time, timedelta original_time = time(14, 30) # Fill in your time, this problem is time sensitive! seconds_to_add = 3600 # Place your seconds here, time won't wait! new_time = (datetime.combine(datetime.today(), original_time) + timedelta(seconds=seconds_to_add)).time() print(new_time) # No time like the present to print the future!

Even if Cinderella's carriage turns into a pumpkin at midnight, this solution has you covered.

Timedelta: Your time travel device

In Python's time zone, the datetime.timedelta is our trusty tool for hopscotching through time. Let's check it out:

  • Sprint through seconds: Add seconds swiftly using the seconds argument.
  • Minutes matter: For minutes, simply convert to seconds (60 seconds = 1 minute) and presto—you're hopping through minutes!
  • Midnight magic: When you cross the midnight boundary, timedelta has your back. It automatically rolls into the next day like clockwork.

However, have in mind that datetime.time object has no understanding of dates. So if our time traveling causes the date to change, we need to employ datetime.datetime for safe passage.

A custom function to fuel your time machine

For some, timedelta is the way to time travel. But for those daredevils who want to build their own time machines, here's a custom addSecs function:

def addSecs(tm, secs): fulldate = datetime(100, 1, 1, tm.hour, tm.minute, tm.second) fulldate = fulldate + timedelta(seconds=secs) return fulldate.time()

Here, we summon an arbitrary date, add time to it, and then magically strip away the date part again, leaving just the time key to our machine!

Time travel comes with caveats

As with all great adventures, temporal excursions have their quirks, such as the illusive leap seconds. There be dragons here: standard libraries like datetime may not slay these creatures, but fear not! A hero called dateutil can combat such beasts.

Alternative time travel methods

Every time traveler has their own tricks. Here are a few chronomanipulation methods you might find handy:

Dateutil: The Swiss army knife

The dateutil module is like that handy gadget that does everything. It can parse time from strings, leap over seconds, and even bake you a nice apple pie (just kidding, or am I?).

Arrow: Your fluent multilingual chauffeur

Have you ever wanted to ask for the time in 15 different languages? No? Well, Arrow can do it anyway. It simplifies localization, formatting, and just about everything else when it comes to datetime manipulation.

Pendulum: Swing your way through time

If you like the idea of pendulums and time (grandfather clocks, anyone?), you'll love Pendulum. It's got goodies like timezone awareness and helper functions that make your datetime playdates a whole lot simpler.

Always remember the conditions of your journey

Daylight saving time turns your carriage back into a pumpkin and time zone changes can warp your perception of time. Be an alert time traveler and handle these anomalies gracefully.

Think about the efficiency of your vessel

If you plan to add seconds en masse, consider the efficiency of your time machine. Every nanosecond counts, so invest wisely in caching and efficient library methods.