This page contains:

Overview

In CommCare you can do calculations with dates and times just like you would with numbers or strings, and there are a few CommCare Functions that are useful for doing so. The key ones explained here are:

  • date() - Takes an input and converts it to be of type date (input must be of type string, numeric, or date)
  • today() - Returns the current date, according to the mobile device.

For Web Apps it returns the current server date, but in the browser's time zone. E.g.:

server time = 2021-10-18 00:01 UTC

browser time zone = ET

today() = 2021-10-17

  • now() - Returns the current date and time, according to the mobile device.  
For Web Apps it returns the current server date/time value, but in the browser's time zone. E.g.:

server time = 2021-10-18 11:50 UTC

browser time zone = ET

now() = 2021-10-18 06:50 ET

More details about these functions, and other functions that may be useful for performing calculations with dates, can be found here.

For further guidance on doing calculations with dates and times, see Hidden Values Tutorial Part 2: Calculations Using a Date.

Examples

Converting date values to type Date

One important concept to understand about dates in CommCare is that a date value can often stored in a string or numeric format, rather than as a Date type, but any date computations need to be performed on arguments that are actually of the type date. So when you have a date value that may be of type string or numeric rather than of type date, you can use date() to convert it before doing computations. Some examples:

Doing calculations with the now() function

In CommCare, you can use the now() function to get the current date and time.

To perform any computations using now(), make sure to consider the following:

Because the result of now() contains time data in addition to date data, you cannot do math or comparisons on this data type directly, and you also should not convert it to a date using date(), or the time portion will be dropped. Instead, you must first convert it to a numeric representation, using the function double(), and then do any desired computations.

Similarly, if you are saving the value of now() as a case property, you must convert it to a number before saving it as a case property, i.e. double(now()). This will convert the now() value to a number that represents the date/time in days since January 1, 1970. Saving now() directly will only save the date portion without the time portion.

As an example of doing computations using now(), suppose you want to compute the amount of time a user spends doing some activity/workflow. You could achieve this by doing the following:

The result is a decimal value in days/fractions of days. To convert to the # of hours (rounded down), you could use the following calculation:

int(#form/time_worked * 24)


To convert now() to a date and time in excel:

To extract a nicely-formatted time from now():

Create a hidden value called double_now to store the value of now():

double(now())

Create a hidden value called seconds_since_midnight to store only the time, represented as the number of seconds since midnight:

(#form/double_now - floor(#form/double_now)) * 24 * 60 * 60

You can then create an hours hidden value:

floor(#form/seconds_since_midnight div 60 div 60)

And a minutes hidden value:

floor((#form/seconds_since_midnight - (#form/hours * 60 * 60)) div 60)

And a seconds hidden value:

floor(#form/seconds_since_midnight mod 60)

And finally display the time nicely:

concat(#form/hours, ':', #form/minutes, ':', #form/seconds)

Note that this will display the time in 24-hour format and will not zero-pad, so 2:05:30 in the afternoon would display as 14:5:30. You can use if statements in the formatted time calculation to address this.

Date Formatting

To convert or format a date into another format (e.g. day of week, name of month, etc.) see the format-date function.

When you enter day, month, and year in as three separate numerical responses, you can convert those three responses into one date as follows

Calculations using time question types

You can use time collected in forms to do different calculations, e.g. the number of hours between the time of birth and now. Some things to keep in mind when doing these calculations: 


To perform calculations with the results of date & time questions and now(), you need to first convert the values from the questions into decimals, in order for them to be compatible with double(now()).
In the following example, we show how to compute the number of hours since time of birth (this is done assuming that you are collecting date and time as 2 different question types to trigger different calculations; similar calculations can also be triggered using the date/time question type, which collects both together):
  1. Collect date and time of birth in date and time questions in your form (call these #form/dob and #form/time_of_birth)
  2. Calculate minutes since midnight for the time of birth (call this #form/minutes_since_midnight_birth)

    substr(#form/time_of_birth, 0, 2) * 60 + substr(#form/time_of_birth, 3, 5)


  3. Convert minutes since midnight into a decimal, by dividing it by the total number of minutes in a day (call this #form/time_of_birth_decimal):

    #form/minutes_since_midnight div 1440


  4. Sum that numeric time with a numeric version of the dob. This will get you to a numeric datetime value like 16925.50833333 (call this #form/birth_datetime):

    double(#form/dob) + #form/time_of_birth_decimal


  5. Then, in the form where you need the hours calculation, subtract your datetime from double(now()) and convert to hours (call this #form/hours):

    (double(now()) - #form/birth_datetime) * 24


  6. If you wish, you can also use the round() function to output a rounded # of hours: 

    round(#form/hours)



Additional Resources