You can use the following date and time functions in your fields' display text or in the values of your skip or validation logic.
Function | Description | Example |
---|---|---|
today() | Returns a string with today’s local date. | =today() |
now() | Returns the current date and time including timezone offset. | =now() |
format-date(date value, string format) | Returns the provided date value formatted as defined by the format argument using the following identifiers: %Y: 4-digit year, %y: 2-digit year, %m 0-padded month, %n numeric month, %b short text month (Jan, Feb, etc), %d 0-padded day of month, %e day of month, %a short text day (Sun, Mon, etc). | =format-date($mydate, "%a, %e %b %Y") returns a date that looks like Tue, 1 Jan 2019. |
format-date-time(dateTime value, string format) | Returns the provided dateTime value formatted as defined by the format argument using the same identifiers as format-date (see above) plus the following: %H 0-padded hour (24-hr time), %h hour (24-hr time), %M 0-padded minute, %S 0-padded second, %3 0-padded millisecond ticks. | =format-date-time(now(), "%H:%M:%S") returns the current time in the format HH:MM:SS |
date(* value) | Converts to a string in the current date format. | Dates stored as text would need to be converted to date format in order to use the date functions. |
decimal-date-time(dateTime value) | Converts dateTime value to the number of days since January 1, 1970 UTC. | |
decimal-time(time value) | Converts time value to a number representing a fractional day in the device’s timezone. For example, noon is 0.5 and 6pm is 0.75. |
See here for more functions and expressions you can use.
Tip: if your calculation does not seem to work correctly, confirm that all brackets include an opening and a closing bracket, and try adding white space around the operator, e.g. instead of "=($field_1+5)" use "=($field_1 + 5)"
Examples:
The fieldworker must record the date of the interview and validation must be added to ensure that no date in the future is accepted for this field.
- Click on the Validation tab of the field.
- Select the comparison type to be 'exactly or before'.
- For the value to compare against, type '=' then select today().
Example of calculating the number of weeks and/or days between two dates:
- Once you've added two date fields in your form, add a calculation field to calculate the number of weeks:
=(($Date2 - $Date1)/7)
- To round it off with a specified number of decimal places you can use the function =round(number arg, number decimals?)
e.g =round((($Date2 - $Date1)/7), 0)
- To calculate the number of days between the two dates, subtract the two dates from each other:
=($Date2 - $Date1)
If you want to display the value of the calculation, add another field (e.g. an Information field) and reference the Calculated Value field name using the dollar character ($) followed by the field name, e.g. $Calc_weeks.
Select the Preview button from the toolbar to test your calculation and see how it will be displayed when using the mobile app.