How to compute the time spent in a form

commcare

Today, you can compare now() and a specific time. In the current meta data there are two relevant values available in form data:

  • started_time
  • completed_time

For example:

I started completing a form on March 20th 2017 at 14:00:00
I submitted a form on March 20th 2017 at 14:05:05
I want to calculate the time it took to complete the form.

Can CommCare calculate the difference in duration for date and time form information?

Can I apply logic to see if the time of the interaction was more than five minutes, and return a true false response?

Can I use these properties to apply logic inside of the form?

Can this information be saved to a case property?

Is it possible to reference these values inside a form?

Can I have to use an integer function?

If yes, can I see the number of seconds that the user took to complete the form?

Best Answer

You cannot directly access timeStart and timeEnd within a form.

However to figure out how much time has passed during a form, you can use a workaround by leveraging Default Values and Calculate Conditions

If you create a hidden value, time_start, with the Default Value being double(now()) this will be your start time since Default Values are only computed once and at the beginning of a form. Now create another hidden value, time_end, with the Calculate Condition being double(now()). Since Calculate Conditions are evaluated every time you answer a question, that time will continually update.

Now the elapsed time in a form is simply time_end - time_start. This will give you the output in days, which is somewhat unfortunate. In order to get to minutes you can do:

(time_end - time_start) * 24 * 60

If that value is greater than 5, then more than 5 minutes has passed. Once that value is saved to a hidden value, you can use it in all the ways you expect (saving to a case, and using in calculations). Hope that helps!