How to calculate no of business days between two date?

Rahul Chaudhary
Tera Contributor

Hi Team,

could you please help to find number of business days between two dates in ServiceNow?

9件の返信9

Ankur Bawiskar
Tera Patron
Tera Patron

@Rahul Chaudhary 

you can use Schedule and handle it

I am also sharing a script

var diff;
var startDate = new GlideDateTime('startDate');
var endDate = new GlideDateTime('endDate');
var sched = new GlideSchedule('38fa64edc0a8016400f4a5724b0434b8'); // sysId of the schedule
diff = sched.duration(startDate,endDate).getDurationValue();
gs.addInfoMessage(diff);

Also check the docs

Using DurationCalculator to calculate a due date

DurationCalculator calcScheduleDuration Mystery

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

thankyou for sharing this code.

i am using your code which you provide in answer with replace by my schedule. but i am getting like that in below

00:00:00

 could you please guide me what i am wrong in this code?

var diff;
var startDate = new GlideDateTime('2023-10-11 09:36:07');
var endDate = new GlideDateTime('2023-10-18 09:36:07');
var sched = new GlideSchedule('602c74208706b9504bf9a6890cbb35aa'); // sysId of the my schedule
diff = sched.duration(startDate,endDate).getDurationValue();
gs.info("new code===="+diff);

Hi @Rahul Chaudhary Can you check in cmn_schedule table whether your schedule sysid is valid?

Regards
Harish

Yes Harish, It is valid please refer screenshot of that.

 

RahulChaudhary_0-1697772501286.png