diff --git a/Server-Side Components/Script Includes/Calculate Business days dynamically/readme.md b/Server-Side Components/Script Includes/Calculate Business days dynamically/readme.md new file mode 100644 index 0000000000..f242856585 --- /dev/null +++ b/Server-Side Components/Script Includes/Calculate Business days dynamically/readme.md @@ -0,0 +1,20 @@ +# Business Day Calculator for ServiceNow + +This JavaScript function calculates a future date by adding a specified number of **business days** (excluding weekends) to a given date. + + +## Features +- Skips weekends (Saturday and Sunday) +- Works with any number of business days +- Uses ServiceNow's `GlideDateTime` API + +## Usage +1. Copy the function into a **Script Include**, **Business Rule**, or **Scheduled Job** in ServiceNow. +2. Call the function with: + - A valid date string (e.g., `'2025-10-24 12:00:00'`) + - The number of business days to add (e.g., `5`) + +## Example +```javascript +gs.print(add_business_days('2025-10-24 12:00:00', 5)); +// Output: 2025-10-31 (skips weekend) diff --git a/Server-Side Components/Script Includes/Calculate Business days dynamically/script.js b/Server-Side Components/Script Includes/Calculate Business days dynamically/script.js new file mode 100644 index 0000000000..ce30c3720b --- /dev/null +++ b/Server-Side Components/Script Includes/Calculate Business days dynamically/script.js @@ -0,0 +1,18 @@ +// Function to add any number of business days to a given date +function add_business_days(date, num_days) { + var result_date = new GlideDateTime(date); + var added_days = 0; + + while (added_days < num_days) { + result_date.addDaysUTC(1); + var day_of_week = result_date.getDayOfWeekUTC(); + if (day_of_week != 6 && day_of_week != 7) { // Skip Saturday (6) and Sunday (7) + added_days++; + } + } + + return result_date.getDate(); +} + +// Example usage: +gs.print(add_business_days('2025-10-24 12:00:00', 5)); // Adds 5 business days