dateDiffInDays( )
Calculate the date difference between two dates measured in days.
The dateDiffInDays( ) 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 days.
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
dateDiffInDays(toDate, fromDate)
Parameters
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 days. 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-7 Difference between two Date items
// Given 2 form questions of type DateTime are defined in the rule as variables:
return dateDiffInDays(datetem1, dateItem2);
Example 3-8 Difference between two hard-coded dates
var toDate = new Date("March 1, 2020");
var fromDate = new Date("March 1, 2019");
return dateDiffInDays(toDate, fromDate);
// Returns value: 366 (leap year!)
Parent topic: Date and time functions