How to display how long it takes to complete a form in CommCare by displaying a now() function

commcare

I'd like to display how long it takes a user to complete a form within the form. Can I use the double (now()) in a hidden value to capture a timestamp? If I do, what timestamp does it actually capture? the moment the form was opened? closed?

Best Answer

In order to capture both the time that a form was started and ended, you can do the following:

  • For the start time, add a hidden value to your form and set the default value to now(). Since default values only fire once when a form opens, this will give you the start time.
  • For the end time, it's a little bit tricker. Adding a hidden value to the form with now() as its calculate condition is not sufficient, because the computation of that value is not guaranteed to re-fire at the end of the form. In order to guarantee this, you need to make the calculate condition dependent upon the last question in the form in some way. The most straightforward way to do this is to have your calculate condition be:

if (/data/last_question_in_form != '', now(), '')

This solution can be imperfect in a couple of ways:

  1. If you have multiple questions in your form that can end up being the last question (due to display logic), you will need to make the conditional statement 'OR' together all of those questions
  2. If the last question in your form is not required, you will need to select the closest-to-last question which is, in order to ensure that this calculate condition will fire.

A good way to get around both of these difficulties would be to add a dummy required last question to your form. It could be just a checkbox question with a single answer choice that says something like:

You are done with the form now. Press "FINISH" to exit