Convert Duration: Hours to Days, Hours, Minutes, and Seconds
Formula
To convert a duration from hours, minutes, seconds to days, hours, minutes, and seconds, use the following formula:
Code
function convertDuration(hours, minutes, seconds) {
const totalSeconds = hours * 3600 + minutes * 60 + seconds;
const totalMinutes = totalSeconds / 60;
const totalHours = totalMinutes / 60;
const totalDays = totalHours / 24;
const days = Math.floor(totalDays);
const hours = Math.floor(totalHours % 24);
const minutes = Math.floor(totalMinutes % 60);
const seconds = totalSeconds % 60;
return {
days,
hours,
minutes,
seconds
};
}
Example
const duration = convertDuration(10, 20, 30);
console.log(duration); // { days: 1, hours: 10, minutes: 20, seconds: 30 }