Date calculations can be used in a form in order to calculate the age of something in days, and from the days, months and/or years. This could be useful for something like:
- The age of an object or person
- Expiration dates
- Days / Time since the last visit to a location
Date calculations can also calculate a number of hours over multiple days, however this article covers simply how to calculate an age. To find out how to calculate hours over a number of days, click the link below:
To get this to work, there are specific settings that are required so that the calculation will be successful.
First, the object names (What is an Object Name?) of the Date fields must match those that are within the calculation. In this example, we will use:
- Starting date = startDate
- Ending date = finishDate
For the initial date calculation, which will calculate the days, the option called 'Advanced Calculation' must be checked.
Then you will need to copy the following code inside the 'Calculation' box (making sure that the object names match those that you've named in the date fields:
"[startDate]"; "[finishDate]";
var startdate = (strtotime(formObject.returnObjectSP("startDate", this.repeatedCount)[0].getValue())-0);
var enddate = (strtotime(formObject.returnObjectSP("finishDate", this.repeatedCount)[0].getValue())-0);
var timediff = myNS.helper.time_diff( date('Y/m/d H:i', startdate), date('Y/m/d H:i', enddate) );
value = (timediff.days);
This will calculate the days between those dates. If you wish to calculate years, then you will need to add another calculation to the form. You are also able to uncheck the 'Visible' and 'Appear in PDF' options within the first date calculator if you don't want it to be seen in the form or report - you will still be able to use the information it calculates in the final calculation.
Calculating Years
To calculate the years from the initial date calculation, check the 'Advanced Calculation' box again, and in the 'Calculation' box, type the object name of the initial calculation divided by 365.25 (this accounts for leap years as best as possible). In the current example this would look like:
[formCalculate_1] / 365.25
The fields incorporated in this date calculation can be structured in different ways:
As regular fields in the form:
Ideal for single entries.
In a Table:
Ideal for multiple entries.
Or as a Repeatable (Repeatable Field or Repeatable Subheader):
Same functionality as a table, but in a different layout in the form template and final PDF report.