- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 02:44 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 03:04 AM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 02:50 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 02:56 AM
@Mark Wood , Refer to this link
https://www.servicenow.com/community/itsm-forum/adding-days-excluding-weekends/m-p/474066
Regards,
Shyamkumar
Regards,
Shyamkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 03:04 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 03:15 AM
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