dateDiffInYears( )
Calculate the difference between two dates in years.
The dateDiffInYears( ) helper function is
invoked with starting and ending dates passed in as parameters. The function returns
a negative or positive number value indicating the difference between the two dates
in years.
Note:
The order in which parameters are supplied for date helper functions is important; the resulting return value depends on which date you pass in as the first or second parameter.
This function is only used to compare variables of type date that do not
contain time elements and do not include partial dates. When using a date/time type
parameter, function considers only date part and ignores time elements.
Tip:
- You can use the timeDiffInMinutes( ) helper function to compare two date and time items.
- If the date question contains partial date elements then use the getDatesCompareResult( ) helper function.
Syntax
dateDiffInYears(toDate, fromDate)
Parameters
Note:
You must compare dates that have the same format.Parameter | Required/Optional | Description |
---|---|---|
toDate |
Required | Ending date value. |
fromDate |
Required | Starting date value. |
Return value
Number that represents the difference between the passed-in dates in years. This number can be positive or negative.
- If the number value returned is a negative or zero value, means
that
toDate
is before or the same as thefromDate
. - If the function returns a positive value,
toDate
is after thefromDate
.
Examples
Example 3-5 Difference between two Date items
// Given 2 form questions of type Date (with no time elements) are defined in the rule as variables:
return dateDiffInYears(dateItem1, dateItem2);
Example 3-6 Difference between two hard-coded dates
var toDate = new Date("March 1, 2020");
var fromDate = new Date("March 1, 2000");
return dateDiffInYears(toDate, fromDate);
// Returns value: 20
Parent topic: Date and time functions