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

AbhishekGardade
Giga Sage

Hello Swapnil,

dataDiff returns you difference in seconds. What output you got? What is the type of u_time_to_resolve?

Sample code I have written :

var grTask = new GlideRecord('sc_req_item');
grTask.addQuery('sys_id','5eec52291b3e3300364d32a3cc4bcb08');
grTask.query();
if(grTask.next()){
gs.print('Number'+grTask.number+"|| Created ON: "+grTask.due_date);
var gdt1 = new GlideDateTime(grTask.due_date);
gs.print("CreatedDate: "+gdt1.getDate());

var gdt = new GlideDateTime('2019-07-22 03:56:25');
gs.print("GDT:"+gdt.getDate());

var startDate = gdt1.getDate(); // Replace u_start_date with your field name of Start Date
var currentDate = gdt.getDate();

var diffSeconds = gs.dateDiff(currentDate.getDisplayValue(),startDate.getDisplayValue(), true);
var daysIn = (diffSeconds/(60*60*24));

gs.print(daysIn );
}

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

Hi Abhishek,

 

Thanks for your response . Well no value was pushed in the field and type of the field is duration .

 

 

Hello Swapnil,

Can you please try with my code? also add logs to check where you are issue.
Thanks,

Abhishek

 

Thank you,
Abhishek Gardade

Alikutty A
Tera Sage

Hi,

You should set your third parameter to false to return a duration data. Please try that once and see if it works

Parameters
NameTypeDescription
startDateStringThe starting date to compare in the current user's date format.
endDateStringThe ending date to compare in the current user's date format.
numericValueBooleanIf true, the return value will be formatted in number of seconds; if false the return value will be formatted ddd hh:mm:ss.