Date difference in scoped application

aparnateja
Kilo Explorer

Hi All,

I want the difference of two date fields (to_date - from_date) in scoped application. I tried using dateDiff() method, but it was showing error that dateDiff is not allowed in scoped applications.

Could any one of you please help me in this regard.

Thanks in advance.

Thanks,

Aparna

7 REPLIES 7

Simon46
ServiceNow Employee
ServiceNow Employee

Hello Aparna,



Maybe the use of the class "GlideDateTime" and the function "subtract" is what you're after.


https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_ScopedGlideDateTimeSubtract_GlideDa...



Alternatively the use of "GlideDuration" may also be of use, as discussed by Cory.


https://community.servicenow.com/thread/193835#817614



Kind regards,


Simon Liang


Hi Simon,



I tried the below code. But it was getting error.



var t = current.to_date;


  var f = current.from_date;


var dur = GlideDateTime.subtract(t, f); //the difference between gdt1 and gdt2


gs.info(dur.getDisplayValue());



error_leave_business_rule.PNG


Thanks,


Aparna


Simon46
ServiceNow Employee
ServiceNow Employee

Hello Aparna,



Can you try the following for me and let me know if it works.



var t = new GlideDateTime(current.to_date);


var f = new GlideDateTime(current.from_date);


var dur = GlideDateTime.subtract(t, f);


gs.info(dur.getDisplayValue());



Kind regards,


Simon Liang


Hi Simon,



It was not displaying any value and when I modified the last line as


gs.addInfoMessage(dur);



It was showing error.



Thanks,


Aparna