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 Alikutty,

Tried that as well , no luck..

sachin_namjoshi
Kilo Patron
Kilo Patron

Check below useful blog for date field calculations

 

https://community.servicenow.com/community?id=community_blog&sys_id=467c62e1dbd0dbc01dcaf3231f9619ad

 

Regards,

Sachin

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

Hi Ankur,

Tried it..no luck

Hi Swapnil,

Can you share screenshot of what error you are receiving?

Regards

Ankur

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