I have a time as a number of milliseconds and I want to convert it to a HH:MM:SS
format. It should wrap around, with milliseconds = 86400000
I want to get 00:00:00
.
Convert Milliseconds to minutes using the formula: minutes = (milliseconds/1000)/60). Convert Milliseconds to seconds using the formula: seconds = (milliseconds/1000)%60). The print output from Milliseconds to minutes and seconds.
To convert a second measurement to a millisecond measurement, multiply the time by the conversion ratio. The time in milliseconds is equal to the seconds multiplied by 1,000.
How about creating a function like this:
function msToTime(duration) { var milliseconds = Math.floor((duration % 1000) / 100), seconds = Math.floor((duration / 1000) % 60), minutes = Math.floor((duration / (1000 * 60)) % 60), hours = Math.floor((duration / (1000 * 60 * 60)) % 24); hours = (hours < 10) ? "0" + hours : hours; minutes = (minutes < 10) ? "0" + minutes : minutes; seconds = (seconds < 10) ? "0" + seconds : seconds; return hours + ":" + minutes + ":" + seconds + "." + milliseconds; } console.log(msToTime(300000))
To Convert time in millisecond to human readable format.
function msToTime(ms) { let seconds = (ms / 1000).toFixed(1); let minutes = (ms / (1000 * 60)).toFixed(1); let hours = (ms / (1000 * 60 * 60)).toFixed(1); let days = (ms / (1000 * 60 * 60 * 24)).toFixed(1); if (seconds < 60) return seconds + " Sec"; else if (minutes < 60) return minutes + " Min"; else if (hours < 24) return hours + " Hrs"; else return days + " Days" } console.log(msToTime(1000)) console.log(msToTime(10000)) console.log(msToTime(300000)) console.log(msToTime(3600000)) console.log(msToTime(86400000))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With