add days to a datetime field

Mark Wood
Tera Contributor

Hello experts,

I'm seeking guidance on how to add 5 days excluding weekends to a custom field with a datetime data type. Could someone please provide instructions on how to achieve this?

1 ACCEPTED SOLUTION

aniket_hadron
Giga Expert

Hello,

You can achieve this using business rule. Create a new BR and set the "when to run" condition to whenever you want the rule to trigger. And use the following script: 

 

(function executeRule(current, previous /*null when async*/) {
    // Define a function to add business days to a given date
    function addBusinessDays(startDate, daysToAdd) {
        var currentDate = new GlideDateTime(startDate);
        var addedDays = 0;

        while (addedDays < daysToAdd) {
            currentDate.addDays(1);

            // Check if the current day is not a weekend (Saturday or Sunday)
            if (currentDate.getDayOfWeek() != 1 && currentDate.getDayOfWeek() != 7) {
                addedDays++;
            }
        }

        return currentDate;
    }

    // Set the custom field to the calculated date
    var daysToAdd = 5; // Change this value based on your requirement
    var newDate = addBusinessDays(current.getValue('name_of_your_datetime_field'), daysToAdd);
    current.setValue('name_of_your_custom_field', newDate);
})(current, previous);

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@Mark Wood Please refer to this article https://www.servicenow.com/community/developer-articles/add-business-days/ta-p/2324391 and see if you can apply the similar approach. 

shyamkumar VK
Kilo Patron

@Mark Wood , Refer to this link 

 

https://www.servicenow.com/community/itsm-forum/adding-days-excluding-weekends/m-p/474066

 

Regards,

Shyamkumar

 

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

aniket_hadron
Giga Expert

Hello,

You can achieve this using business rule. Create a new BR and set the "when to run" condition to whenever you want the rule to trigger. And use the following script: 

 

(function executeRule(current, previous /*null when async*/) {
    // Define a function to add business days to a given date
    function addBusinessDays(startDate, daysToAdd) {
        var currentDate = new GlideDateTime(startDate);
        var addedDays = 0;

        while (addedDays < daysToAdd) {
            currentDate.addDays(1);

            // Check if the current day is not a weekend (Saturday or Sunday)
            if (currentDate.getDayOfWeek() != 1 && currentDate.getDayOfWeek() != 7) {
                addedDays++;
            }
        }

        return currentDate;
    }

    // Set the custom field to the calculated date
    var daysToAdd = 5; // Change this value based on your requirement
    var newDate = addBusinessDays(current.getValue('name_of_your_datetime_field'), daysToAdd);
    current.setValue('name_of_your_custom_field', newDate);
})(current, previous);

Maddysunil
Kilo Sage

@Mark Wood 

You can write your script like below:

 

// Get the current date
var currentDate = new Date();

// Initialize variables for counting added days and weekends
var addedDays = 0;
var targetDate = new Date(currentDate);

// Loop through to find the target date excluding weekends
while (addedDays < 5) {
    // Add a day to the target date
    targetDate.setDate(targetDate.getDate() + 1);

    // Check if the day is not a weekend (Saturday or Sunday)
    if (targetDate.getDay() !== 0 && targetDate.getDay() !== 6) {
        addedDays++;
    }
}

// Set the result to your custom field
current.record.setValue('your_custom_field', targetDate);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks