How to skip the weekends and consider only weekdays to set due date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2019 02:42 AM
Hi,
I am setting due date based in SLA days and created date i.e. if i am creating a ticket the due date has to be set dynamically based on the SLA days and created date.
I am able to get the due date with the help of client script and script include. But it's counting the weekends as well. I need the due date to exclude weekends and take only the weekdays(5 working days (Monday to Friday)) into count.
REF:: Below is the script
Script include:
MyDateTimeAjax = Class.create();
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor,
{
nowDateTime: function ()
{
gs.log("MyDateTimeAjax");
var createdDate = new GlideDateTime(gs.nowDateTime());
var nd = new GlideDateTime(gs.nowDateTime());
var dueDateValue = "";
var days = 0 ;
gs.log("nd value is" + nd);
var cat = this.getParameter('sysparm_cat');
var subcat = this.getParameter('sysparm_subcat');
var itm = this.getParameter('sysparm_itm');
var gr = new GlideRecord("u_sla_admin_details");
gr.addQuery("u_category", cat);
gr.addQuery("u_sub_category", subcat);
gr.addQuery("u_item", itm);
gr.addQuery("u_record_type", "ADMIN");
gr.addQuery("u_active", "true");
gr.query();
if(gr.next())
{
var slaResult = (gr.u_ttr * 1 );
var slaCall = "";
if (gr.u_sla_days != "")
{
slaCall = (gr.u_sla_days * 1);
gs.log("value of days before increment is " + days);
if(nd.getDayOfWeekUTC() == 6)
{
nd.addDaysLocalTime(2);
}
else if(nd.getDayOfWeekUTC() == 7)
{
nd.addDaysLocalTime(1);
}
gs.log("nd value before incrementing us: " + nd);
nd.addDaysUTC(slaCall);
gs.log("Value of nd after incrementing us: " + nd);
while (createdDate < nd)
{
if (createdDate.getDayOfWeekUTC() != 6 && createdDate.getDayOfWeekUTC() != 7)
//excluding Weekends
gs.log("value of createdDate if not 6 and 7 is: " + createdDate);
{
gs.log("value of days before increment is " + days);
days++ ;
gs.log("value of days after increment is " + days);
}
createdDate.addDaysLocalTime(1);
gs.log("createdDate Value is: " + createdDate);
}
gs.log("createdDate Value completed is: " + createdDate);
gs.log("nd Value completed is: " + nd);
if (createdDate.getDayOfWeekUTC() == nd.getDayOfWeekUTC())
{
gs.log("createdDate Value after processing: " + createdDate);
return createdDate;
}
else if(createdDate.getDayOfWeekUTC() == 6 )
{
createdDate.addDaysLocalTime(2);
return createdDate;
}
else if(createdDate.getDayOfWeekUTC() == 7)
{
createdDate.addDaysLocalTime(1);
return createdDate;
}
//var duedate = nd;
//gs.log("createdDate value is: " + createdDate);
//gs.log("duedate value is: " + duedate);
/*while (createdDate < duedate)
{
if (createdDate.getDayOfWeek() != 6 && createdDate.getDayOfWeek() != 7)
//excluding Weekends
{
gs.log("value of days before increment is " + days);
days++ ;
gs.log("value of days after increment is " + days);
}
createdDate.addDays(1);
}*/
//gs.log("Days value is: " + duedate);
//return duedate;
//return nd;
}
else
{
slaCall = "Not Applicable";
nd = slaCall;
gs.log("Inside MyDateTimeAjax else part nd value: " + nd);
return nd;
}
}
}
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2019 02:54 AM
You can use schedule to make this easier, create an schedule which runs 24*5 weekdays and use it your script
Your schedule should look like this
Then use this schedule in your script
var startDate = new GlideDateTime();
var days = 10;
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days); // your duration goes here
var schedule = new GlideSchedule('e1cc05f1db7440104c22e1bb4b96192f'); //schedule sys_id
var end = schedule.add(startDate, dur);
gs.info(startDate.getDisplayValue());
gs.info(end.getDisplayValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2022 06:45 PM
I had a similar requirement. This works perfectly. Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2019 03:23 AM
you can use the Duration calculation to calculate due date by skipping required days.
check the below link for details :
-Satheesh