I want to convert milliseconds and have 2:00 output. It should look like duration of the song.
I was trying with this code:
import datetime
seconds = milliseconds/1000
b = int((seconds % 3600)//60)
c = int((seconds % 3600) % 60)
dt = datetime.time(b, c)
print(dt)
>>> 02:30:00
Do you have another ideas? Or maybe I should change something in my code.
Edit: I solved the problem with following code
ms = 194000
seconds, ms = divmod(ms, 1000)
minutes, seconds = divmod(seconds, 60)
print(f'{int(minutes):01d}:{int(seconds):02d}')
What about using a simple divmod
? That way, minutes > 59 are possible and no imports needed, e.g.
milliseconds = 86400001 # a day and a millisecond... long song.
seconds, milliseconds = divmod(milliseconds, 1000)
minutes, seconds = divmod(seconds, 60)
print(f'{int(minutes):02d}:{int(seconds):02d}.{int(milliseconds):03d}')
# 1440:00.001