GlideSystem - dateDiff(String startDate, String endDate, Boolean numericValue)

Calculates the difference between two dates.

This method expects the earlier date as the first parameter and the later date as the second parameter; otherwise, the method returns the difference as a negative value. Use getDisplayValue() to convert the strings to the expected format.

This method expects parameters in the user/system date time format, which may not be the same as the internal format. Using parameters in formats other than the user/system date time format may return invalid results.

If you are working with GlideDateTime objects use the GlideDateTime subtract() method instead of dateDiff().

Table 1. Parameters
Name Type Description
startDate String The starting date to compare in the current user's date format.
endDate String The ending date to compare in the current user's date format.
numericValue Boolean If true, the return value will be formatted in number of seconds; if false the return value will be formatted ddd hh:mm:ss.
Table 2. Returns
Type Description
String If the numericValue parameter is true, returns the difference between the two dates as an integer number of seconds; if false, returns the difference between the two dates in the format ddd hh:mm:ss.

// Given two date/times as DateTime objects
// Set the values this way to ensure a consistent input time
var date1 = new GlideDateTime();
var date2 = new GlideDateTime();
date1.setDisplayValueInternal('2014-01-01 12:00:00');
date2.setDisplayValueInternal('2014-01-01 13:00:00');
 
// Determine the difference as number of seconds (returns a string)
// Use getDisplayValue() to convert the string to the format expected by dateDiff()
var diffSeconds = gs.dateDiff(date1.getDisplayValue(), date2.getDisplayValue(), true);
 
// JavaScript will coerce diffSeconds from a string to a number
// since diffSeconds is being compared to a number
var msg = (diffSeconds <= 0) ? ' is on or after ' : ' is before ';
gs.print(date1.getDisplayValue() + msg + date2.getDisplayValue())