Time Calculation Between Fields

Hi all, I’d like to be able to calculate the hours and minutes based on a number of units entered, where 1 unit equals 6 minutes. For example, if a user entered 5 units in the Units field, then the Minutes dropdown would show 30 minutes, but if they entered 12 units, then the Hours dropdown would show 1 hour and the Minutes dropdown would show 12 minutes.

To complicate matters, I’d like this to work in reverse too. So if a user selected 36 minutes from the Minutes dropdown, then the Units field would show 6 units, etc.

I’m not sure where to start to achieve this, so any help with this would be much appreciated!

Thanks!

TimeCalc.rp (55.2 KB)

Here’s a working version I threw together in 10 minutes, so it might not have everything covered. There were some behaviors left unspecified so I just filled in the gaps myself. For example, when setting minutes, units are rounded down to the nearest whole unit.

I also have the units update on click of a button instead of using the OnTextChange event because I use Set Text to update the field from the dropdown, which causes the event to fire, which causes strange behavior. This can be worked around but it’s extra work so I leave that as an exercise to you.

Here’s the important calculations:
The number of hours is equal to the number of units times six, divided by 60, and rounded down to the nearest whole number.
hours = [[Math.floor((units * 6) / 60)]]

The number of minutes is the number of units times 6, modulo 60.
minutes = [[(units * 6) % 60]]

If you look at the dropdown actions you’ll see I’m basically treating the conversion between time and units as strictly minutes to units (this keeps the conversion simple), and then converting the total number of minutes to hours and remaining minutes for display purposes only.

hours-minutes-units.rp (54.9 KB)

Thanks nkrisc, that’s great.

I’ve noticed that occasionally when editing the Units field, the value reverts to a previously entered value when you hit the button. Is there a way to stop this?

Thanks!

Hi again, one other question. How would I make it round up to the nearest 6 minute unit? For example, if I enter 20 minutes it should round up to 4 units, not down to 3 units.

Thanks

Add 1 to it inside of the Math.floor() function.

For example:
Math.floor(2.23) = 2
Math.floor(2.23 + 1) = 3

That might be the behavior you want, but you should just play around with it.

I haven’t noticed the issue you mentioned, but I didn’t look very hard either. The next time I have some time I’ll take a look.