Formatting duration in h, min, sec

I have two scenarios when I want to display duration in hours, minutes and seconds.
What would be best practices for displaying them in human readable format?

  1. Displaying duration using datetime difference between two datetime values:
DATETIME_DIFFERENCE(NOW(),, "hours") + " h  "  + DATETIME_DIFFERENCE(NOW(),, "minutes")  + " min  " + DATETIME_DIFFERENCE(NOW(),, "seconds")  + " s"

  1. When duration is already saved to db in milliseconds.

Are there any build in ways to show it or does it need to be calculated?

I did not find a built in function, but could realize it with javascript:

The output of the javascript block is then used in a formular to concat the final text

1 Like

Thanks @Andreas_Becker!
This works well if data is saved to db with fields for hours, minutes & seconds.
Otherwise it would be tricky to show them in repeated elements.

Hey, as you can see in the first image, the input of the javascript is in my case fixed, but could also come from a database. The output might also be flexible. In my case i have an object with hours, minutes and secons but it could also be changed to have a concated string (so this does not need to be done in a second formula)

yeah I get it! Works well with values that have been locked in. Now it’s just a matter of solving first problem when other date is NOW(). I think the formula would work better with repeated items to.

Yeah, your formula for the dates need to substract the hours from the minutes and the hours and minutes from the seconds.

I get that but I’m finding difficult to build that formula.

Hey @Mevi. I try to figure out this out that it would work with repeated items. There isn’t way to run js for each repeated group item so this needs to be in formula?

Also do I need to set the looped currentDateTime page variable for the formula or can I just use NOW()?

You could run JavaScript for the whole set of data I guess? But I prefer to use formulas yep.

Um, you could use NOW()? I don’t see why not.

I was able to create a formula for this:

"Duration  " + DATETIME_DIFFERENCE(pageVars.currentDateTime,, "hours") + " h  "  + DATETIME_DIFFERENCE(SUBTRACT_DURATION(pageVars.currentDateTime, DATETIME_DIFFERENCE(pageVars.currentDateTime,, "hours"), "hours"),, "minutes")  + " min  " + DATETIME_DIFFERENCE(SUBTRACT_DURATION(pageVars.currentDateTime, DATETIME_DIFFERENCE(pageVars.currentDateTime,, "minutes"), "minutes"),, "seconds")  + " s  "
1 Like

@Mevi Just noticed an issue on iOS: when I leave the app and return, the application stops execution of all logic flows or NOW() inside a formula. Is there something I need to do the get the app running again?

Yeah iOS tends to stop logic when app is in background. Use page focused event to start the cycle again, or some other event that fires when app is opened again.