
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on ‎07-27-2020 01:43 AM
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:
- Date Validations - Client Scripts
- Select only Business Days excluding holidays Using Schedules
- Select Weekdays in the date field
Mark the article as helpful and bookmark if you found it useful.
Regards,
Asif
2020 ServiceNow Community MVP
- 14,927 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Just for clarity, I'd probably label the variables differently:
var hours = days * 8;
var dur = new GlideDuration(60 * 60 * 1000 * hours);

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hey Jim,
yep makes sense 🙂
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I need to validate that the date is 3 BUSINESS days from today. If no, then it shows an error msg. I've tried variations of your code but not sure how to set the script include to return a true or false (not sure how to write that if statement), then I can use my client script to perform appropriately.
Much appreciated!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
We have requirement to auto populate the date field in the catalog item ,it should add 7 business days with current date also excluded with weekends and holidays.
So, As you mentioned, I have created schedules then script includes and Onload client script .but it is not working.
Can we use same script in OnLoad instead of Onchange.I am getting like "javascript script error in browser console"
Please give your suggestion here.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
In the background script this all works fine but in the scheduled script job it doesn't work. It crashes at the step
var end = schedule.add(startDate, dur);
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I created a Weekdays 5/24 schedule and used it in below background script.
If the startDate is a Thursday, the output is always a Saturday. It should be following Monday.