Add Business Days
Hello
In this article I will show you how to add business days to a given selected date. You can do this from 2 ways one from server script and another through client script.
System Scheduler -> Schedules
First you need to either create a schedule or use any existing schedule which meets your requirements from here.
1. Server Script only (through BR)
//If you have to update the end date based on start date and no. of days field
on submit of the form then use the below BR
var startDate = new GlideDateTime(current.start_date);
var days = parseInt(current.no_ofdays);
//assuming there are 8 business hours
days = days*8;
var dur = new GlideDuration(60 * 60 * 1000 * days);
var schedule = new GlideSchedule('472acc8d1b521c1012c0dc6cdc4bcbb2'); //put your schedule sys_id here
var end = schedule.add(startDate, dur);
current.end_date=end;
2. Update the end date on selection of start date (through Client Script and Script Include)
Client Script (OnChange on your start Date)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax("addBusinessDays");
ga.addParam('sysparm_name','addDays');
ga.addParam('sysparm_days', g_form.getValue('u_days'));
ga.addParam('sysparm_date', newValue);
ga.getXML(processResponse);
function processResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer").toString();
g_form.setValue('work_end', answer);
}
}
Script Include
var addBusinessDays = Class.create();
addBusinessDays.prototype = Object.extendsObject(AbstractAjaxProcessor, {
addDays: function() {
var days = parseInt(this.getParameter("sysparm_days"));
var startDate = new GlideDateTime(this.getParameter("sysparm_date").toString());
days = days * 8;
var dur = new GlideDuration(60 * 60 * 1000 * days);
var schedule = new GlideSchedule('472acc8d1b521c1012c0dc6cdc4bcbb2'); //put your schedule sys_id here
var end = schedule.add(startDate, dur);
end = end.getDate().getByFormat("dd/MM/yyyy")+" "+end.getTime().getByFormat("hh:mm:ss");
return end;
},
type: 'addBusinessDays'
});
Things to Note:
1. In case your scheduled is all Day and not by timings, then
//remove this line
days = days * 8;
replace the below line
var dur = new GlideDuration(60 * 60 * 1000 * days);
with
var dur = new GlideDuration(60 * 60 * 1000 * 24 * days);
Let me know if you have any questions in the comments below.
Related Date articles that I Wrote:
Mark the article as helpful and bookmark if you found it useful.
Regards,Asif
https://www.servicenow.com/community/developer-articles/add-business-days/ta-p/2324391