timeDiffInHours( )
Calculate the time difference between two date or date/time values in hours.
The timeDiffInHours( ) 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 hours.
When using date type variables with no time elements,
function considers time as '00:00:00'.
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.
Syntax
timeDiffInHours(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 hours. 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-9 Difference between two date/time items
// Given 2 form questions of type DateTime are defined in the rule as variables:
return timeDiffInHours(dateTime1, dateTime2);
Example 3-10 Difference between two hard-coded date/time items
var toDate = new Date("March 1, 2020 13:00:00");
var fromDate = new Date("March 1, 2020 12:00:00");
return timeDiffInHours(toDate, fromDate);
// Returns value: 1
Example 3-11 Difference between 2 time items
var toDate = new Date( '01-Jan-0001 ' + ruleTimeItem.getHour() + ':' + ruleTimeItem.getMinute() + ':' + ruleTimeItem.getSecond() );
var fromDate = new Date( '01-Jan-0001 ' + ruleTimeItem2.getHour() + ':' + ruleTimeItem2.getMinute() + ':' + ruleTimeItem2.getSecond() );
return timeDiffInHours(toDate, fromDate);
Example 3-12 Difference between two partial date items
var toDate = new Date( ruleTimeItem.getYear() + '-' + ruleTimeItem.getMonth() + '-' + ruleTimeItem.getDay() + ' ' + ruleTimeItem.getHour() + ':' + ruleTimeItem.getMinute() + ':' + ruleTimeItem.getSecond() );
var fromDate = new Date( ruleTimeItem.getYear() + '-' + ruleTimeItem.getMonth() + '-' + ruleTimeItem.getDay() + ' ' + ruleTimeItem.getHour() + ':' + ruleTimeItem.getMinute() + ':' + ruleTimeItem.getSecond() );
return timeDiffInHours(toDate, fromDate);
Parent topic: Date and time functions