Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to check if two dates are equal using gs.dateDiff function ?

venishawakodika
Giga Contributor

how to check if two dates are equal using gs.dateDiff function ?

what does it return if dates are equal ?

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Hi Vineetha,



Please find below details and helpful link.



calDateDiff(String, String, boolean)Calculate the difference between two dates using the default calendar. Note: Calendars are now legacy. If Schedules are being used, see Calculate Duration Given a Schedule.


dateDiff(String, String, boolean)


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. Note: Use getDisplayValue() to convert the strings to the expected format.


3.13.1 Input Fields

Parameters:


  • startDate - a starting date to compare, in the current user's date format.
  • endDate - an ending date to compare, in the current user's date format.
  • boolean bnumericValue - true to return difference in number of seconds as a string, false to return difference in the format ddd hh:mm:ss.

3.13.2 Output Fields

Returns: if boolean bnumericValue is true, the difference in number of seconds; if false, the difference in the format ddd hh:mm:ss.


3.13.3 Example

For more examples, see Setting the Duration Field Value.


// 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())


GlideSystem Date and Time Functions - ServiceNow Wiki


http://wiki.servicenow.com/index.php?title=Calculate_Duration_Given_a_Schedule


View solution in original post

6 REPLIES 6