How to convert time milliseconds to hours, min, sec format in JavaScript?

后端 未结 16 2346
渐次进展
渐次进展 2020-11-29 02:28

I have the time as milliseconds, but I want the time after conversion like 00:00:00.

Ex: In milliseconds=86400000. I want how many hours in that millise

16条回答
  •  抹茶落季
    2020-11-29 03:07

    Human-readable code for human-readable output and you can extend this to light years or nanoseconds or what have you very intuitively. Obviously you'd want to convert this to a function and re-use some of those intermediate modulo calls.

    second = 1000 
    minute = second * 60
    hour = minute * 60 
    day = hour * 24
    
    test = 3 * day + 2 * hour + 11 * minute + 58 * second
    
    console.log(Math.floor(test / day))
    console.log(Math.floor(test % day / hour))
    console.log(Math.floor(test % day % hour / minute))
    console.log(Math.floor(test % day % hour % minute / second))
    

提交回复
热议问题