From a2cc67930dfba0abe699e93f198097a203c3ea0d Mon Sep 17 00:00:00 2001 From: Bharath Date: Fri, 24 Oct 2025 10:09:26 -0500 Subject: [PATCH 1/2] Create script.js --- .../script.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Server-Side Components/Script Includes/Calculate Business days dynamically/script.js 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 From c1f5208e410d2e8703620024b5dfdd89cce7a4e1 Mon Sep 17 00:00:00 2001 From: Bharath Date: Fri, 24 Oct 2025 10:11:08 -0500 Subject: [PATCH 2/2] Create readme.md --- .../readme.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Server-Side Components/Script Includes/Calculate Business days dynamically/readme.md 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)