How to add two time in python

I'm really disappointed if there is not any more pythonic solution... :(

Horrible one ->

timeList = [ '0:00:00', '0:00:15', '9:30:56' ]

ttt = [map(int,i.split()[-1].split(':')) for i in timeList]
seconds=reduce(lambda x,y:x+y[0]*3600+y[1]*60+y[2],ttt,0)
#seconds == 34271

This one looks horrible too ->

zero_time = datetime.datetime.strptime('0:0:0', '%H:%M:%S')
ttt=[datetime.datetime.strptime(i, '%H:%M:%S')-zero_time for i in timeList]
# delta==datetime.timedelta(0, 34271)

# str(delta)=='9:31:11' # this seems good, but 
# if we have more than 1 day we get for example str(delta)=='1 day, 1:05:22'

Really frustrating is also this ->

sum(ttt,zero_time).strftime('%H:%M:%S')  # it is only "modulo" 24 :( 

I really like to see one-liner so, I tried to make one in python3 :P (good result but horrible look)

import functools
timeList = ['0:00:00','0:00:15','9:30:56','21:00:00'] # notice additional 21 hours!
sum_fnc=lambda ttt:(lambda a:'%02d:%02d:%02d' % (divmod(divmod(a,60)[0],60)+(divmod(a,60)[1],)))((lambda a:functools.reduce(lambda x,y:x+y[0]*3600+y[1]*60+y[2],a,0))((lambda a:[list(map(int,i.split()[-1].split(':'))) for i in a])(ttt)))
# sum_fnc(timeList) -> '30:40:11'

1import datetime as dt
2t1 = dt.datetime.strptime('12:00:00', '%H:%M:%S')
3t2 = dt.datetime.strptime('02:00:00', '%H:%M:%S')
4time_zero = dt.datetime.strptime('00:00:00', '%H:%M:%S')
5print((t1 - time_zero + t2).time())

Skip to content

How to add two different times in Python?

import datetime as dt
t1 = dt.datetime.strptime('12:00:00', '%H:%M:%S')
t2 = dt.datetime.strptime('02:00:00', '%H:%M:%S')
time_zero = dt.datetime.strptime('00:00:00', '%H:%M:%S')
print((t1 - time_zero + t2).time())


add 2 times in python, add multiple at the same timepython a,b, add time hhmmsspython, add times together python, add two time in python, add two times in python, add two times together python, adding two times in python, apend a value multiple times python, append multiple times in python, append multiple times python, create datetime object using year, month and date python, datetime.time python sum, display time as month python 3, how to add times in python, how to add two different times in python, how to add two type of times together in python, how to calculate in python year day with time module, how to change the starting date in dt python, how to get time 6 months from now python timezone, how to print a number multiple times in python, is there a way to convert 1s1m1h into a datetime delta python, list of past 24 hours in python, ppend somehting 2 times python, python add two times, python add two times the same number and then go one up, python add two variables at the same time, python append multiple times, python date format increase weightage in data, python how many times add chane value, python sum of timestamp, python time sum, reading file give date object with time in python, set a var to time using datetime python, sum all hours and min in python, sum time in python, sum time python, the correct type for a date in python, time period python not working, time sum x hour in python, update a list of old datetime objects to today

Change font size

Post navigation

How do you add time 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) .

How do you add HH mm in Python?

Firstly, we will Import 'datetime' and 'timedelta' from datetime module, Then we will store our Present time in a variable. After that, we will align date in “HH:MM:SS” format. Now we can print our Present time.

How do you add 1 second to time in Python?

Use the timedelta() class from the datetime module to add seconds to datetime, e.g. result = dt + timedelta(seconds=24) .

How do I add hours and minutes to a time in Python?

To add minutes to the current time in Python:.
Use the datetime. now() method to get the current local date and time..
Create a timedelta object by passing the number of minutes to the timedelta class..
Sum the current time and the timedelta object..