Reputation: 305
Is it possible to convert int into hours:min:sec
import datetime
x = 40000
t = int(x)
day = t//86400
hour = (t-(day*86400))//3600
min = (t - ((day*86400) + (hour*3600)))//60
seconds = t - ((day*86400) + (hour*3600) + (min*60))
hello= datetime.time(hour.hour, min.minute, seconds.second)
print (hello )
I want this Output:- 11:06:40
Upvotes: 8
Views: 35219
Reputation: 131
Note: In response to @Pavel's answer,
You may find time.gmtime(x)
not giving the right time as shown on your pc.
To produce a time consistent with what is on your pc, use time.localtime(x)
instead
Upvotes: 1
Reputation: 7562
You can also outsource all the division and modulo operations to Python built-ins (remember: batteries are included!)
>>> import time
>>> x = 40000
>>> time.strftime('%H:%M:%S', time.gmtime(x))
'11:06:40' # <- that's your desired output
>>> time.gmtime(x)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=11, tm_min=6, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)
>>> time.gmtime(x).tm_hour # <- that's how to access individual values
11
Upvotes: 12
Reputation: 81654
You almost got it.
hour
, min
and seconds
are integers, and integers don't have hour
, minute
or second
attributes.
Change
hello = datetime.time(hour.hour, min.minute, seconds.second)
to
hello = datetime.time(hour, min, seconds)
As a side note, t = int(x)
is totally unnecessary as x
is already an int
.
As a side note 2, in the future please provide the error you receive.
Upvotes: 5