-1

I have tried different functions I found in SO but none give me a precise output. I did one variant of this (adding days and changing to d:h:m)

const convertMinsToHrsMins = (mins) => {
  let h = Math.floor(mins / 60);
  let m = mins % 60;
  h = h < 10 ? '0' + h : h;
  m = m < 10 ? '0' + m : m;
  return `${h}:${m}`;
}

my last attempt:

  // Convert Minutes to Days Hours Minutes
  const convertMinutes = (totalMinutes) => {
    let Days = Math.floor((totalMinutes / 1440) % 60)
    let Hours = Math.floor(totalMinutes / 60)
    let Minutes = Math.round(totalMinutes % 60)
    let ret = ''
    if (Days > 0) {
      ret += '' + Days + 'd ' + (Hours < 10 ? '0' : '')
    }
    ret += '' + Hours + 'h ' + (Minutes < 10 ? '0' : '')
    ret += '' + Minutes + 'm'

    return ret
  }

totalMinutes receive a sum of different inputs(all in minutes!). I need this function to work as close as precise as possible. e.g.: convertMinutes(totalminutes) 937d 23h 59m 8d 00h 01m

Rafael GS
  • 5
  • 1
  • Are you able to use a library like `momentjs`. Makes your life a lot easier when dealing with dates. – codemonkey Jan 18 '21 at 20:30
  • 1
    Please include what questions you've looked at. A [quick search](https://stackoverflow.com/search?q=%5Bjavascript%5D+convert+minutes+to+days%2C+hours%2C+and+minutes) yields 152 results. It is hard to believe that an answer could not be found within those. – Heretic Monkey Jan 18 '21 at 21:16
  • @codemonkey thank you, I’ll probably use this library in this project as it deals a lot with time. – Rafael GS Jan 19 '21 at 07:42
  • @HereticMonkey as you probably know or not, it’s not normal to keep code or tabs opened that is not useful. And definitely I’m not going through my history. – Rafael GS Jan 19 '21 at 07:45

2 Answers2

1

Convert 1 day, 1 hour and 1 minute into minutes; subtract one day from your input till you can't anymore; then subtract one hour from your input till you can't anymore; then subtract one minute from your input till you can't anymore or either return the rest. You have to respect that order. Here is a function that mets your necessity:

function converter(minutes)
    {
    dates=[1440,60,1]
    output=[0,0,0];
    for(x=0; x<3; x++)
        {
        while(minutes>=dates[x])
            {
            minutes-=dates[x]
            output[x]++
            }
        }
    return output[0]+"Days;"+output[1]+"Hours;"+output[2]+"Minutes."
    }
Maga2020
  • 11
  • 1
  • 4
  • That's a very strange indentation style that makes it somewhat difficult to read. With JavaScript's automatic semicolon insertion rules, it is generally not a good idea to have open curly brackets on the next line, for instance. Also, without the use of the `var`, `let`, or `const` keywords, all of your variables become global, which could cause problems down the road. Just some thoughts to ponder... – Heretic Monkey Jan 18 '21 at 21:20
  • Thanks for the tips. What do you mean by having curly brackets on the next line? Do you mean like this: for(x=0; x<3; x++) { } Instead of this: for(x=0; x<3; x++){} Why is that a problem? By the way I think my code is not strange actually. I am a sort of boomer and I use identation to see what belongs to what no matter who long it may look. I am not into those symbols dynamic people use such as $:?. – Maga2020 Jan 18 '21 at 22:41
  • Thank you, this worked for me. Needed to declare the variables with let and worked just fine. – Rafael GS Jan 19 '21 at 07:49
1

Use division to get the days, then use modulo to get the remaining minutes which don't sum up to a full day. Use the remaining minutes and and do the same with hours (division and modulo).

const convertMinutes = (totalMinutes) => {
    const minutesInDay = 24 * 60;
    const minutesInHour = 60;
    let days = Math.floor(totalMinutes / minutesInDay);
    let remainingMinutes = totalMinutes % minutesInDay;
    let hours = Math.floor(remainingMinutes / minutesInHour);
    let minutes = remainingMinutes % minutesInHour;

    return `${days}d:${hours}h:${minutes}`;
}
Heiko
  • 11
  • 1