How to efficiently use Date/Time functions

on September 12, 2016

In this blog post I will be discussing an approach you can use when utilizing date/time functions. When using microflows to calculate differences in time, it can quickly become confusing to know if the functions you are using will produce the correct time output. Another common issue observed, especially if you are calculating a difference in time by minutes, is that you need to do some additional calculations to correctly display the seconds along with minutes.


In this example I will be creating a microflow that calculates the average amount of time it takes a user to complete a task (assume the user is using a task management app). This time needs to be displayed in the format of Xmin Ysec, where X is a variable that displays the average time for minutes and Y the average time for seconds, as seen below:


At first glance this could be a daunting task, because you would need to separate the average time into two variables while also making sure the time correctly displays the time for both minutes and seconds. The below picture displays the microflow that will handle this:

While this might not look like a very complicated microflow, the last action button requires a bit of complexity and can also be the root cause of a major headache to the developer if not configured correctly. In order to calculate the two time variables mentioned above you will need to create two functions that calculate the following:

  1. Calculate the average time to complete a task, then round that number down so the end result is the average time minute value.
  2. Calculate the average time to complete a task and then subtract the average minute value calculated above, so the end result is the average time second value.

To accomplish this, we will utilize the two microflow functions below:

  1. Round – Rounding a floating-point number, optionally to a specified precision
  2. Floor – Rounding a floating-point number down

Below is a screenshot of the correct attribute calculations for the microflow action to use for reference:

Debugging Tips

When debugging the above attribute calculations, it is best to break down each section of each IF statement by creating a variable in the microflow that only contains that specific section of the calculation. This method allows you to set accurate debugger breakpoints in the modeler where you can easily discover which section of your equations are incorrect. Below is an example of how to debug the above attributes:
Each highlighted selection is to be replicated in its own variable so the user can see, through the debugger, if any of those functions are calculating an incorrect value.
As you can see above, five specific variables were created in order to capture each function of the calculated attributes. What you would then do is add a debugger breakpoint to the first section and see what the calculated value is. If it is an acceptable value, you can move on to the second variable. If the variable is not and accepted value, you can work on that section of the code. This process will allow you to discover exactly where your mistake is, and reduce the amount of frustration when you would normally calculate the complicated attributes and then not know where to start if their value were incorrect.

In Conclusion

Utilizing the date/time functions provided in the Mendix modeler can be a powerful way to visually display user metrics on completing tasks, provided you go through a methodical way of debugging your code.

  • Very nice example, a small request which has been on my mind for a few other blogs as well. Since MX introduced the model share function why are you still using images instead of the nice model share include option? The model share would allow everyone to get an indepth look at the example you created.

  • Good point Mitchel. Modelshare added 🙂