We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Adding days excluding weekends

Shaik Abdul Mu1
Tera Expert

Hi Team,

 

I have a requirement where I need to add no.of days to approved date excluding weekends and show the same on a date field. For this I'm trying below background script but it is not going inside " if " after for loop. Let me know if any mistakes and modifications required.

 

var due = '3';
var approved = '2020-05-23 11:57:41';
var gdt = new GlideDateTime(approved);
gs.print(gdt);

for(var i=0; i<due;i++){
var schedule = new GlideSchedule("08fcd0830a0a0b2600079f56b1adb9ae")
gs.print('inside for');
if(schedule.isInSchedule(gdt)){
gs.print('if');
gdt.addDays(1);
}
else{
gdt.addDays(1);
}
}

if(i==due){
gs.print(gdt.getDate());
}

 

 

Any quick response will be appreciated. Thanks in advance.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

Hi,

So based on 8-5 Weekdays schedule you need to add 3 days and exclude weekends from that?

Since 8-5 Weekdays doesn't include weekends you need not check that

please use below script; it should give 4th june

var days = 3;
var approved = '2020-05-23 11:57:41';

var approvedGdt = new GlideDateTime(approved);

gs.info(approvedGdt);

var dur = new GlideDuration(60*60*24*1000*days);
var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
var finalTime = schedule.add(approvedGdt, dur);
var value = finalTime.getDate();

gs.info(value); // output as 2020-06-04

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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Priyanka Chandr
Mega Guru

Hi,

Refer this link

https://community.servicenow.com/community?id=community_question&sys_id=48b84361db5cdbc01dcaf3231f96...

Please mark it correct and helpful if applicable.

Thanks,