Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Get Date difference

laszlobolya
Kilo Expert

Hello, I would like to get a date difference in days between a Date field an Today date in Business Rule in scoped app.

It try this:

var start = current.startdate;

  gs.addInfoMessage(start);

var today = new GlideDate();//.getDisplayValue();

  gs.addInfoMessage(today);

     

var diff = gs.dateDiff(start, today, true);

gs.addInfoMessage(diff);

I get the error: Function dateDiff is not allowed in scope x_63963_cognizant. Use GlideDateTime.subtract() instead

If I use the line: var diff = GlideDateTime.subtract(start, today);

OR just GlideDate

I also get an error, object GlideDateTime not found.

Could you help me please how get this? All I need is a number value in days between the stardate field and the current date.

Many thanks!

4 REPLIES 4

Ankur Bawiskar
Tera Patron

Hi Laszlo,



Following links will be helpful to you:


Scoped GlideDateTime API Reference - ServiceNow Wiki



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


Thanks


Ankur


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

amlanpal
Kilo Sage

Hi Laszlo,



Could you please try the below one:


var start = current.startdate;


var gdt1 = new GlideDateTime();


gdt1.setDisplayValue(start);  


var today = new GlideDate().getDisplayValue();


var dur = new GlideDuration();


dur = GlideDateTime.subtract(today,gdt1).getDayPart();


gs.info(dur);



I hope this helps. Please mark correct/helpful based on impact


anshul_jain25
Kilo Guru

Hi Laszlo,



Did you get code for your solution,


If yes can you please share , i am also looking for this type of code.


Greg42
Mega Guru

Hi Laszlo,



Try this:



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



    var gdStartDate = new GlideDate();


    var gdEndDate = new GlideDate();



    gdEndDate.setValue(current.your_date_field);



    current.date_diff_in_days = GlideDate.subtract(gdStartDate, gdEndDate).getDayPart();



})(current, previous);




Regards



Greg