Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Time conversion h:m:s to seconds

I am aware that with the timedelta function you can convert seconds to h:m:s using something like:

>> import datetime
>> str(datetime.timedelta(seconds=666)) 
'0:11:06'

But I need to convert h:m:s to seconds, or minutes.

Do you know a function that can do this?

like image 299
Geparada Avatar asked May 24 '12 17:05

Geparada


1 Answers

def hms_to_seconds(t):
    h, m, s = [int(i) for i in t.split(':')]
    return 3600*h + 60*m + s
like image 101
Hugh Bothwell Avatar answered Oct 20 '22 19:10

Hugh Bothwell