From milliseconds to hour, minutes, seconds and milliseconds

前端 未结 9 1856
失恋的感觉
失恋的感觉 2020-12-07 21:22

I need to go from milliseconds to a tuple of (hour, minutes, seconds, milliseconds) representing the same amount of time. E.g.:

10799999ms = 2h 59m 59s 999ms

9条回答
  •  遥遥无期
    2020-12-07 21:51

    not really eleganter, but a bit shorter would be

    function to_tuple(x):
       y = 60*60*1000
       h = x/y
       m = (x-(h*y))/(y/60)
       s = (x-(h*y)-(m*(y/60)))/1000
       mi = x-(h*y)-(m*(y/60))-(s*1000)
    
       return (h,m,s,mi)
    

提交回复
热议问题