The CreatorCon Call for Content is officially open! Get started here.

Difference between 2 date fields

Arjun34
Kilo Contributor

Hello All,

 

I wrote a before business rule where I need a calculate difference between 2 fields and put the duration value in another field :

field 1 : 'sys_created_on'

field 2 : 'u_resolved_time'

and duration to be copied on 'u_time_to_resolve' 

Wrote the below script but it is not working :

var startDate = new GlideDateTime(current.sys_created_on);

var endDate = new GlideDateTime(current.u_resolved_time);

current.u_time_to_resolve = gs.dateDiff(startDate.getDisplayValue(),endDate.getDisplayValue(),true);

Can anybody help to understand the miss here ?

Thanks a lot in advance!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Swapnil,

try this script once:

var startDate = new GlideDateTime(current.sys_created_on);

var endDate = new GlideDateTime(current.u_resolved_time);

var diff = gs.dateDiff(startDate.getDisplayValue(),endDate.getDisplayValue(),true); // this would return seconds

current.u_time_to_resolved.setDateNumericValue(diff*1000); // this method accepts milliseconds

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

16 REPLIES 16

Hi Ankur,

 

It worked!! Thanks a lot for your help .

Naveen105
Mega Contributor

Hi swapnil,

 

Please find the script below, please try this script and make this answer correct if it works for you.

 

Client Scripting:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (newValue === '') {
      return;
   }
   var end = g_form.getValue("u_end_date");
   var start = g_form.getValue("u_start_date");
  var ga = new GlideAjax('yeardate');
  ga.addParam('sysparm_name','yeardate');
  ga.addParam('sysparm_end',end);
  ga.addParam('sysparm_start',start);
  ga.getXML(callback);
  function callback(response)
  {
   var answer = response.responseXML.documentElement.getAttribute('answer'); 
   g_form.setValue('another_fieldname',answer);

}
   

Script Include:

var yeardate = Class.create();
yeardate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
 yeardate : function()
 {
 var s = this.getParameter('sysparm_start');
 var e = this.getParameter('sysparm_end');
 var a = gs.dateDiff(s,e,true);
 return a;
 },

    type: 'yeardate'
});

 

 

Swapnil, make sure that the field type is DATE/TIME.

Hello The below script is working for me. Please check the below screenshot for reference.

find_real_file.png

it works..!!