Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting minutes to days, hours, and remaining minutes

Tags:

javascript

Trying to convert minutes per total number of episodes into days, hours, and minutes but I can't figure out the proper formula for hours.

function minToDays(min, ep) {
  let days = parseInt((min * ep) / 1440);
  let hours = parseInt(((min * ep) % 60));
  let remainingMin = hours % 60;
  return `${days} day(s) and ${hours} hour(s) and ${remainingMin} minutes(s).`;
}
console.log(minToDays(20, 800)); //11 days, 2 hours, 40 minutes
like image 754
Ron Rance Avatar asked Oct 30 '25 01:10

Ron Rance


1 Answers

A bit clunky, but here's a quick way to see the logic - each time you need to see what minutes remain. I'm sure someone with more time will give you a more elegnat answer:

function minToDays(min, ep) {
  let days = Math.floor((min * ep) / 1440);
  let remainingTime = parseInt((min*ep) - Math.floor((days *1440)));
  let hours = Math.floor((remainingTime / 60));
  let remainingMin = Math.floor(remainingTime - (hours*60));
  return `${days} day(s) and ${hours} hour(s) and ${remainingMin} minutes(s).`;
}
console.log(minToDays(20, 800)); //11 days, 2 hours, 40 minutes
like image 144
sideroxylon Avatar answered Nov 01 '25 16:11

sideroxylon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!