ApathyBear
ApathyBear

Reputation: 9595

Python: Convert subtracted unix time into hours/mins/seconds

I have received three different unixtimes

lastSeen = 1416248381 
firstSeen = 1416248157

and the last one is lastSeen - firstSeen:

duration = 224

Now, I can convert lastSeen and firstSeen into a datetime no problem. But I am having trouble with the duration.

I am unsure how to convert the duration into something like minutes/seconds. Does anyone have any idea if this can be done?

Upvotes: 4

Views: 8514

Answers (1)

AlvaroAV
AlvaroAV

Reputation: 10553

You need to convert your seconds to Hours&Minutes and you can do it using datetime

import datetime

lastSeen = 1416248381 
firstSeen = 1416248157
duration = lastSeen - firstSeen

str(datetime.timedelta(seconds=duration))

The ouput will be: '0:03:44'

Without the str() function you'll have: datetime.timedelta(0, 224)


Using time

import time

time.strftime("%H:%M:%S", time.gmtime(duration))

The ouput will be: '0:03:44'

Upvotes: 10

Related Questions