Calculate Duration between start and end date

snuser10
Kilo Expert

Hello - I've planned start date and planned end date in change module.   We want to calculate the duration between the two dates in a seperate field.   Thoughts on BR script? I tried below with condition that date fields aren't blank but it doesn't seem to work.

calculateDiff();

function calculateDiff(){

  var outage_start = g_form.getValue('start_date');

  var outage_end = g_form.getValue('end_date');

  var ajax = new GlideAjax('AjaxDurCalc');

  ajax.addParam('sysparm_name','durCalc');

  ajax.addParam('sysparm_strt',start_date);

  ajax.addParam('sysparm_end',end_date);

  ajax.getXMLWait();

  var answer = ajax.getAnswer();

  g_form.setValue('u_estimated_task_duration', answer);

}

1 ACCEPTED SOLUTION

sachin_namjoshi
Kilo Patron
Kilo Patron

You can use below code to calcuate duration betweeen two dates.



  1. getTimeDiff();  
  2.  
  3.   function getTimeDiff(){  
  4.   var startDate = current.u_start_date.getGlideObject();  
  5.   var endDate = current.u_end_date.getGlideObject();  
  6.  
  7.   current.u_duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);  
  8.  
  9.  
  10.   }  


Regards,


Sachin


View solution in original post

7 REPLIES 7

Shishir Srivast
Mega Sage

Please check if this helps:



(function executeRule(current, previous /*null when async*/) {


var startDate = current.u_date.getGlideObject();


var endDate = current.u_date1.getGlideObject();


current.u_duration = (gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false)).split(' ')[0];


})(current, previous);


nishailame
ServiceNow Employee
ServiceNow Employee

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


 


Thanks.


PS: Hit like, Helpful, Correct and Endorse, if it answers your question.


Sebastian R_
Kilo Sage

If you are in a scoped application you can use the following script because the current answer is not working in scopes.

var gdtStart = new GlideDateTime(current.getValue('u_start_date'));
var gdtEnd = new GlideDateTime(current.getValue('u_end_date'));
 
var duration = GlideDateTime.subtract(gdtStart, gdtEnd); //the difference between gdt1 and gdt2

current.u_duration = duration.getDurationValue();