Adding days excluding weekends

Shaik Abdul Mu1
Giga 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
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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

It is not calculating correctly. For 23/05/2020 expected due date would be 27/05/2020 (added 3 days). In the above script it is giving 04/06/2020.

Hi Shaik,

please correct the script as below; I forgot to make this change

Since your schedule is 9hours a day; we need to use 9 in the GlideDuration;

I have highlighted in bold

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*9*1000*days);
var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
var finalTime = schedule.add(approvedGdt, dur);
var value = finalTime.getDate();

gs.info(value); // output as 2020-05-28

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

Hi Ankur,

 

I need to print 7 days due date in incident resolved notification excluding weekends.

Example: if i resolve the incident on 10-07-2022 then in resolved notification " Due date 18-07-2022 should print.

1. scheduled

2. mail script

var approvedGdt = new GlideDateTime();
approvedGdt.addDaysUTC(7);

//gs.info(approvedGdt);

var dur = new GlideDuration(60*60*24*1000*days);
var schedule = new GlideSchedule('sys id');
var finalTime = schedule.add(approvedGdt, dur);
var value = finalTime.getDate();
var gd = new GlideDate();
gd.setValue(value.toString());
template.print(value.getByFormat("dd-MM-yyyy")); // output as 2020-06-04

})(current, template, email, email_action, event);

 

 

PLease help on this