I have a ServiceNow script to calculate a due date by adding business days (skipping weekends).

tilekarnilesh
Giga Guru

Here is the scenario:

Today is May 5th, 2025.

The script is supposed to add 5 business days, skipping weekends (Saturday and Sunday), and set the due date accordingly.

The expected outcome:

Add 5 business days from May 5th, skipping May 10th (Saturday) and May 11th (Sunday), so the due date should be May 12th, 2025 (Monday).

However, the script is currently calculating the due date as May 10th, 2025, without skipping the weekend.

Here is the code I am using:

javascript
Copy
Edit
(function executeRule(current, previous /*null when async*/) {

// Get the Requested Item (RITM) record
var ritmRecord = current.request_item.getRefRecord();

// Access the variable 'project_category' from the RITM
var variableValue = ritmRecord.variables.project_category;

// If value is 'Standard', set due date to 5 business (week) days later
if (variableValue == 'Standard') {

var businessDaysToAdd = 5;
var daysAdded = 0;

var gdt = new GlideDateTime();

// Start with today's date and time
var startDate = new GlideDateTime();
gdt.setValue(startDate.getValue()); // Retain current date and time

while (daysAdded < businessDaysToAdd) {
gdt.addDaysUTC(1); // Add 1 calendar day at a time

// Get the date portion in UTC and determine the weekday
var gd = new GlideDate();
gd.setValue(gdt.getDate()); // getDate() gives UTC date portion
var dayOfWeek = gd.getDayOfWeek(); // 1 = Sunday, 7 = Saturday (UTC-based)

// Skip weekends (Sunday = 1, Saturday = 7), count only weekdays
if (dayOfWeek != 1 && dayOfWeek != 7) {
daysAdded++;
}
}

// Set the due date in the current record
current.due_date = gdt;
}

})(current, previous);

The issue:

When I run this script, the due date is not skipping the weekend (May 10th and 11th). Instead, it's calculating May 10th, which is a Saturday.

Can anyone explain why this might be happening and suggest how I can fix it to ensure weekends are properly skipped?






2 ACCEPTED SOLUTIONS

J Siva
Tera Sage

Hi @tilekarnilesh 
try the below script.
It'll work as expected.

 
(function executeRule(current, previous /*null when async*/ ) {
    var ritmRecord = current.request_item.getRefRecord();
    var variableValue = ritmRecord.variables.project_category;
    if (variableValue == 'Standard') {

        var gdt = new GlideDateTime();
        gdt.addDaysUTC(5);

        gs.print(gdt.getDate());
        gs.print(gdt.getDayOfWeek());

        if (gdt.getDayOfWeek() == '6') { // If it's Saturday add two more days.
            gdt.addDaysUTC(2);
        }
        if (gdt.getDayOfWeek() == '7') { //If it's Sundau add one more day.
            gdt.addDaysUTC(1);
        }
        current.due_date = gdt;
    }

})(current, previous);

Regards,
Siva

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@tilekarnilesh 

try this

(function executeRule(current, previous /*null when async*/) {

    // Get the Requested Item (RITM) record
    var ritmRecord = current.request_item.getRefRecord();

    // Access the variable 'project_category' from the RITM
    var variableValue = ritmRecord.variables.project_category;

    // If value is 'Standard', set due date to 5 business (week) days later
    if (variableValue == 'Standard') {

        var businessDaysToAdd = 5;
        var daysAdded = 0;

        var gdt = new GlideDateTime();

        // Start with today's date and time
        var startDate = new GlideDateTime();
        gdt.setValue(startDate.getValue()); // Retain current date and time

        while (daysAdded < businessDaysToAdd) {
            gdt.addDaysUTC(1); // Add 1 calendar day at a time

            // Get the date portion in UTC and determine the weekday
            var gd = new GlideDate();
            gd.setValue(gdt.getDate()); // getDate() gives UTC date portion
            var dayOfWeek = gd.getDayOfWeek(); // 1 = Sunday, 7 = Saturday (UTC-based)

            // Skip weekends (Saturday = 6, Sunday = 7), count only weekdays
            if (dayOfWeek != 6 && dayOfWeek != 7) {
                daysAdded++;
            }
        }

        // Set the due date in the current record
        current.due_date = gdt;
    }

})(current, previous);

It worked for me in background scripts

  var businessDaysToAdd = 5;
  var daysAdded = 0;

  var gdt = new GlideDateTime();
  gs.info('now time' + gdt);
  // Start with today's date and time
  var startDate = new GlideDateTime();
  gdt.setValue(startDate.getValue()); // Retain current date and time

  while (daysAdded < businessDaysToAdd) {
      gdt.addDaysUTC(1); // Add 1 calendar day at a time

      // Get the date portion in UTC and determine the weekday
      var gd = new GlideDate();
      gd.setValue(gdt.getDate()); // getDate() gives UTC date portion
      var dayOfWeek = gd.getDayOfWeek(); // 1 = Sunday, 7 = Saturday (UTC-based)

      // Skip weekends (Saturday = 6, Sunday = 7), count only weekdays
      if (dayOfWeek != 6 && dayOfWeek != 7) {
          daysAdded++;
      }
  }
  gs.info('Final date' + gdt);

Output:

 

AnkurBawiskar_0-1746448001470.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

J Siva
Tera Sage

Hi @tilekarnilesh 
try the below script.
It'll work as expected.

 
(function executeRule(current, previous /*null when async*/ ) {
    var ritmRecord = current.request_item.getRefRecord();
    var variableValue = ritmRecord.variables.project_category;
    if (variableValue == 'Standard') {

        var gdt = new GlideDateTime();
        gdt.addDaysUTC(5);

        gs.print(gdt.getDate());
        gs.print(gdt.getDayOfWeek());

        if (gdt.getDayOfWeek() == '6') { // If it's Saturday add two more days.
            gdt.addDaysUTC(2);
        }
        if (gdt.getDayOfWeek() == '7') { //If it's Sundau add one more day.
            gdt.addDaysUTC(1);
        }
        current.due_date = gdt;
    }

})(current, previous);

Regards,
Siva

Ankur Bawiskar
Tera Patron
Tera Patron

@tilekarnilesh 

try this

(function executeRule(current, previous /*null when async*/) {

    // Get the Requested Item (RITM) record
    var ritmRecord = current.request_item.getRefRecord();

    // Access the variable 'project_category' from the RITM
    var variableValue = ritmRecord.variables.project_category;

    // If value is 'Standard', set due date to 5 business (week) days later
    if (variableValue == 'Standard') {

        var businessDaysToAdd = 5;
        var daysAdded = 0;

        var gdt = new GlideDateTime();

        // Start with today's date and time
        var startDate = new GlideDateTime();
        gdt.setValue(startDate.getValue()); // Retain current date and time

        while (daysAdded < businessDaysToAdd) {
            gdt.addDaysUTC(1); // Add 1 calendar day at a time

            // Get the date portion in UTC and determine the weekday
            var gd = new GlideDate();
            gd.setValue(gdt.getDate()); // getDate() gives UTC date portion
            var dayOfWeek = gd.getDayOfWeek(); // 1 = Sunday, 7 = Saturday (UTC-based)

            // Skip weekends (Saturday = 6, Sunday = 7), count only weekdays
            if (dayOfWeek != 6 && dayOfWeek != 7) {
                daysAdded++;
            }
        }

        // Set the due date in the current record
        current.due_date = gdt;
    }

})(current, previous);

It worked for me in background scripts

  var businessDaysToAdd = 5;
  var daysAdded = 0;

  var gdt = new GlideDateTime();
  gs.info('now time' + gdt);
  // Start with today's date and time
  var startDate = new GlideDateTime();
  gdt.setValue(startDate.getValue()); // Retain current date and time

  while (daysAdded < businessDaysToAdd) {
      gdt.addDaysUTC(1); // Add 1 calendar day at a time

      // Get the date portion in UTC and determine the weekday
      var gd = new GlideDate();
      gd.setValue(gdt.getDate()); // getDate() gives UTC date portion
      var dayOfWeek = gd.getDayOfWeek(); // 1 = Sunday, 7 = Saturday (UTC-based)

      // Skip weekends (Saturday = 6, Sunday = 7), count only weekdays
      if (dayOfWeek != 6 && dayOfWeek != 7) {
          daysAdded++;
      }
  }
  gs.info('Final date' + gdt);

Output:

 

AnkurBawiskar_0-1746448001470.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Abhijit4
Mega Sage

Hi @tilekarnilesh 

 

The best and efficient solution to get business days difference is always to use schedule.

 

Here is simplified script for your reference:

 

    // Get the schedule (use the sys_id of your schedule or name)
    var schedule = new GlideSchedule("08fcd0830a0a0b2600079f56b1adb9ae");
	var dur=new GlideDuration(60*60*24*1000*5);
    var gdt = new GlideDateTime();
	gs.print("Todays Date: "+gdt);
    var newDateTime =schedule.add(gdt, dur); // Convert days to milliseconds
    gs.print("5 Business Days: "+newDateTime); // Returns the final date as a string

 

Output:

 

Abhijit4_0-1746450989585.png

 

Schedule has to be all day ( I have changed existing 8-5 schedule to all day and tested )

Abhijit4_1-1746451057476.png

 

You can create your own schedule, which will consider weekdays and 24*7 schedule.

 

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP