# How to generate a time duration from number of minutes

Here’s what I want to do:

I have a duration in minutes. I want to use that number to print out a “time” showing hours and minutes with two digits for each (e.g. 2 minutes would become “00:02”; 110 minutes would become “01:50”).

I’ve been looking over the supported Date, Math and String functions and can’t wrap my head around how I might get this to work. Any suggestions?

1 Like

Take a look at northam’s example here: Stop Watch, Stopwatch, Timer (Having issues creating digital “Stopwatch” timer)

1 Like

That was fast! Thank you!

Hi,
I am trying to implement on my prototype a stopwatch in a format 00(hr):00(min):00(sec). When start on page load it is supposed to start counting seconds and minutes and stop counting when stop button is hit.

I’ve tried to access these links for help but they have been archived.

Thanks
Becks

Use a single variable for your time that stores it represented as the total number of seconds.

Then you can show hours/minutes/seconds with this expression:

`[[('x0' + Math.floor(Timer / 60 / 60)).slice(-2) ]]:[[('x0' + Math.floor(Timer / 60)).slice(-2) ]]:[[('x0' + Timer % 60).slice(-2)]]`

(NOTE: the `slice` and `'x0'` is to make sure there is a leading `0` if any of the units are a single digit number)

To count you just have to add 1 to your variable ever second. I would do this by adding a hidden widget and using an event like Move to add to the variable. Add a condition to check a different variable which will be toggled by your button so you can control whether the timer should stop or not. Then you have a separate case that fires the event again after 1 second. This is your timer.

OnMove
If variable StopCounter does not equal true
Set variable Timer to: [[Timer + 1]]

If true
Wait 1000ms
Fire event this widget Move

Then to top it all off you just need to make your button set StopCounter to “true” and the timer will stop. If you then change StopCounter to be “false” or nothing then it will start counting again.

4 Likes

You are a legend! This was super helpful, Thank you <3

A small modification was required because the minutes were not correct:

[[(‘x0’ + Math.floor(Timer / 3600)).slice(-2) ]] : [[(‘x0’ + Math.floor((Timer-Math.floor(Timer / 3600)*3600) / 60)).slice(-2) ]] : [[(‘x0’ + Timer % 60).slice(-2)]]

1 Like

Not sure what you mean about the minutes. If you’re storing your timer as seconds the expression I have works fine. 5 minutes 30 seconds is 330 seconds. 330 divided by 60 is 5.5, the floor of which is 5, as expected.

Assuming a `Timer` value of 330:
`Math.floor(330/60) == 5`
`Math.floor((330-Math.floor(330 / 3600)*3600) / 60) == 5`
You can test it yourself in the browser console.

The result for low numbers are the same. But in the expression [hr]:[min]:[sec] the minutes were able to raise till 99. This means for example if you enter the value of seconds for 90 minutes you get the result 01:90:00. Therefore, I substracted the amount of seconds used for the hours. Maybe it is not simplest and cleanest way, but it did the job. I don’t fully understand “Timer%60” eather but your “x0”…".slice(-2)" made my day. Thank you

I see what you mean now. Yeah I didn’t originally account for that. Here’s a far simpler way:

`Math.floor((Timer / 60) % 60)`

The `%` operator is the modulus operator and it gives you the remainder after division. Same principle behind getting the seconds value.

2 Likes