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:

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 the fromDate.
  • If the function returns a positive value, toDate is after the fromDate.

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!)