dateDiffInYears( )

Calculate the difference between two dates in years. For instance, when you have two questions of type Date in a form.

The dateDiffInYears( ) helper function is invoked with toDate and fromDate passed in as parameters. The function returns a negative or positive number value indicating the difference between the two dates in years. If the number value returned is a negative or zero value, toDate is before or the same as fromDate. If the function returns a positive value, toDate is after fromDate.

Note:

This function is only used to compare variables of type date that do not contain time elements. When using a date/time type parameter, function considers only date part and ignores time elements.

You can use the timeDiffInMinutes( ) helper function to compare two date and time items.

Syntax

dateDiffInYears(toDate, fromDate)

Note:

You must compare dates that have the same format.

Parameters

toDate

Date value.

fromDate

Date value.

Return value

Difference between the dates in years (number). This number can be positive or negative.

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.

Example 3-4 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-5 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