How to get Due Date and Current Date Difference using BR rule?

Vishal Kumar Sr
Tera Contributor

How to get Due Date and Current Date Difference using BR? I have a scenario where I have a need to get Difference of current date and due date and need to publish via report. I need to show difference in days only. Should I use client script instead of BR?

I tried this :

var date=new GlideDate();

var current=date;

gs.info(current)  //Showing me the current Date

var gr=new GlideRecord('table_name');

var dd=gr.getValue('due_date');  or var dd=gr.getDisplayValue('due_date');

var diff=dd-current;

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi @Vishal Kumar Srivastava ,

this script will work in scoped app and give the difference in days

Note: If you wish to populate the difference in some field when BR runs; the BR should be Before and not After

var currentTime = new GlideDateTime();
var actualDatetime = new GlideDateTime(current.due_date);
var dur = new GlideDuration();
dur = GlideDateTime.subtract(currentTime, actualDatetime);

var days = dur.getDayPart();

Regards
Ankur

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

View solution in original post

11 REPLIES 11

Community Alums
Not applicable

Hi @Vishal Kumar Srivastava ,

Refer to this thread: https://community.servicenow.com/community?id=community_question&sys_id=63108f21db98dbc01dcaf3231f96...

Mark my answer correct & Helpful, if Applicable.

Thanks,
Sandeep

Community Alums
Not applicable

Hi @Vishal Kumar Srivastava ,

Any update to this ?Any follow-up required? if not

Kindly mark the answer as Correct & Helpful both such that others can get help.

Thanks,
Sandeep

Shekhar Navhak1
Kilo Sage

Hi Vishal,

 

Please try below script:

var gr=new GlideRecord('table_name');

var diff = gs.dateDiff(gs.nowDateTime(), gr.due_date.getDisplayValue(), true);   


gs.print(diff/(60*60*24))

 

Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
 
Regards,
Shekhar

Please mark the answer correct/helpful based on Impact.
Regards, Shekhar

Hi Shekhar,

I  tried this dateDiff() in my BR, But it says not supported in this scope.

Thanks & Regards,

Vishal Kumar Srivastava