asifnoor
Kilo Patron

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
2020 ServiceNow Community MVP

Comments
Jim Coyne
Kilo Patron

Just for clarity, I'd probably label the variables differently:

var hours = days * 8;
var dur = new GlideDuration(60 * 60 * 1000 * hours);
asifnoor
Kilo Patron

Hey Jim,

yep makes sense 🙂

Cheri M
Kilo Sage

@asifnoor - These are great, I am still struggling with my requirements. Hopefully you can help.

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!

Jonatan1
Kilo Contributor

@Cheri Hello, I also have the same problem as you are facing - I want to add 3 business days from today, not including todays date. Let me know if you solved this issue.


Thanks.

Anirudh_saraf
Tera Contributor

@Jonatan1 @Cheri M  I have same issue please let me know if its solved thanks 

nandhini muthu
Tera Contributor

@asifnoor 

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.

AJ2025
Tera Contributor

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);
maroon_byte
Mega Sage

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.

 

var startDate = new GlideDateTime('2025-06-19');
var days = 7;
var dur = new GlideDuration(24 * 60 * 60 * 1000 * days);
var schedule = new GlideSchedule('xxx'); // sys_id of the schedule.
var end = schedule.add(startDate, dur);
gs.print('end: ' + end);
 
Please let me know if anyone else came across this issue and its solution.
Version history
Last update:
‎07-27-2020 01:43 AM
Updated by: