From milliseconds to hour, minutes, seconds and milliseconds

前端 未结 9 1874
失恋的感觉
失恋的感觉 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:29

    milliseconds = x
    total = 0
    while (milliseconds >= 1000) {
      milliseconds = (milliseconds - 1000)
      total = total + 1
    }
    hr = 0
    min = 0
    while (total >= 60) {
      total = total - 60
      min = min + 1
      if (min >= 60) hr = hr + 1
      if (min == 60) min = 0
    }
    sec = total
    

    This is on groovy, but I thing that this is not problem for you. Method work perfect.

提交回复
热议问题