Python: convert unix subtracted time to hours / minutes / seconds

I got three different unixtimes

lastSeen = 1416248381 
firstSeen = 1416248157

      

and the last one is lastSeen - firstSeen:

duration = 224

      

Now I can convert lastSeen and firstSeen to datetime without problem. But I'm having problems with the duration.

I'm not sure how to convert the duration to a few minutes / seconds. Does anyone know if this can be done?

+3


source to share


1 answer


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

import datetime

lastSeen = 1416248381 
firstSeen = 1416248157
duration = lastSeen - firstSeen

str(datetime.timedelta(seconds=duration))

      

The output would be: '0:03:44'

Without a function str()

, you would have:datetime.timedelta(0, 224)




Time use

import time

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

      

The output would be: '0:03:44'

+7


source







All Articles