How to refer the date and time of the mobile device

I wanted to refer the current date and time of of the mobile device in a hidden value field of a form , so do we have any option to refer those values in the form.

Please suggest.

We are referring metadata field named " Completion-time" in our data analysis calculation , where we wanted to restrict the mobile users to submit any of the form in case, the Mobile's date and time is not correct.

Thanks and regards,
Gurudayal Sahu


You have a few options for how to do this, depending on what you want to do with the value.

The format-date() function can be used to help extract out elements of the current date and time. For example:

format-date(now(), '%Y-%m-%dT%H:%M:%S') -> 2021-06-11T09:08:53

That example produces an ISO8601 timestamp of the current date and time.

You can also use multiple format-date() calls to pull out the individual components of the date and time.

Another option is to use double(now()) to collect a numeric representation of the current moment. That will produce a numeric output which can be compared more directly (it's recorded in fractional days since the epoch).

I'm not sure either will be helpful enough though if you are trying to suppress behavior based on local clock drift, since the app runs offline and isn't able to confirm the date and time against a global clock. You would, however, be able to prevent users from submitting forms with dates that are outside of some envelope (IE: forms where the current date is before June 2021).

In general, it's been my experience that it's not really possible to identify "just in time" clock problems outside of that range option, but CommCare will try to keep track of problems with the clock during form submissions. If you look at a submitted form's "Form Metadata" tab, you'll note that there is a "drift" field reported. This field denotes any time difference between the local device clock and the server's clock when the form is submitted. The system doesn't try to "correct" for drift, because it's not possible to identify whether the clock was wrong when the form was collected (the clock may change in between filling out a form and submitting a form), but it's a helpful way to keep track of whether users are having pervasive clock issues.


Thanks, Clayton !

This is helpful.

With regards,
Gurudayal Sahu