timeDiffInSeconds( )

Calculate the time difference between two date or date/time values in seconds.

The timeDiffInSeconds( ) 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 seconds.

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.

When using date type variables with no time elements, function considers time as '00:00:00'.

Syntax

timeDiffInSeconds(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 seconds. 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-17 Difference between two date/time items

// Given 2 form questions of type DateTime are defined in the rule as variables:
return timeDiffInSeconds(dateTime1, dateTime2);

Example 3-18 Difference between two hard-coded date/time items

var toDate = new Date("March 1, 2020 12:02:00");
var fromDate = new Date("March 1, 2020 12:00:00");
return timeDiffInSeconds(toDate, fromDate);
 
// Returns value: 120

Example 3-19 Difference between two time items

var date1 = new Date( '01-Jan-0001 ' + ruleTimeItem.getHour() + ':' + ruleTimeItem.getMinute() + ':' + ruleTimeItem.getSecond() );
var date2 = new Date( '01-Jan-0001 ' + ruleTimeItem2.getHour() + ':' + ruleTimeItem2.getMinute() + ':' + ruleTimeItem2.getSecond() );
return areDateTimesEqual(date1, date2);

Example 3-20 Compare two partial date items

var date1 = new Date( ruleTimeItem.getYear() + '-' +  ruleTimeItem.getMonth() + '-' + ruleTimeItem.getDay() + ' ' + ruleTimeItem.getHour() + ':' + ruleTimeItem.getMinute() + ':' + ruleTimeItem.getSecond() );
var date2 = new Date( ruleTimeItem.getYear() + '-' +  ruleTimeItem.getMonth() + '-' + ruleTimeItem.getDay() + ' ' + ruleTimeItem.getHour() + ':' + ruleTimeItem.getMinute() + ':' + ruleTimeItem.getSecond() );
return areDateTimesEqual(date1, date2);