Converting Minutes to Hours and Minutes

I need to calculate total minutes and then display it to the user in an hh:mm format. I haven’t been able to find something to automatically do this. (If you have a way you’re my hero!)

So I’m trying to manually perform the conversion and the math functions aren’t working like expected. Here’s what I have:

The problem is at the arrow. Everything else is working correctly. When I have a total time less than 60 (one hour) I get a 1 for the stakeRunHours value.

Here’s the screen. The Second Stake should run for 0:35 not 1:35.

And the Print statements.

Of course if there’s an easier way to do this via formatting or something I’m all ears!!!
Many Thanks.

Hi @Esteri_Hinman,

In your case, you can add an additional check using if , in order to handle the case when you have less than 60 minutes

Below is an example of how this can be implemented using custom code and a function.

  1. create a custom function
  2. drag the custom code
  3. open the custom code editor and paste the code that I provided below
  4. use as shown in the screenshot

Regards,
Sergey



function formatTime(totalMinutes) {
    const sign = totalMinutes < 0 ? '-' : ''
    const absMinutes = Math.abs(totalMinutes)
    const hours = Math.floor(absMinutes / 60)
    const minutes = absMinutes % 60

    return `${sign}${hours}:${minutes.toString().padStart(2, '0')}`
}

return formatTime(totalMinutes)

Well now I’m having a “Duh” moment. :woman_facepalming: Sometimes you’re too close to the problem and just have to have someone else point out the obvious.

But I like your idea of a custom function. Thanks!!!

Esteri