Skip to content

Commit 9833fca

Browse files
authored
Business Day Calculator for ServiceNow (#2462)
* Create script.js * Create readme.md
1 parent 327db59 commit 9833fca

File tree

2 files changed

+38
-0
lines changed
  • Server-Side Components/Script Includes/Calculate Business days dynamically

2 files changed

+38
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Business Day Calculator for ServiceNow
2+
3+
This JavaScript function calculates a future date by adding a specified number of **business days** (excluding weekends) to a given date.
4+
5+
6+
## Features
7+
- Skips weekends (Saturday and Sunday)
8+
- Works with any number of business days
9+
- Uses ServiceNow's `GlideDateTime` API
10+
11+
## Usage
12+
1. Copy the function into a **Script Include**, **Business Rule**, or **Scheduled Job** in ServiceNow.
13+
2. Call the function with:
14+
- A valid date string (e.g., `'2025-10-24 12:00:00'`)
15+
- The number of business days to add (e.g., `5`)
16+
17+
## Example
18+
```javascript
19+
gs.print(add_business_days('2025-10-24 12:00:00', 5));
20+
// Output: 2025-10-31 (skips weekend)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Function to add any number of business days to a given date
2+
function add_business_days(date, num_days) {
3+
var result_date = new GlideDateTime(date);
4+
var added_days = 0;
5+
6+
while (added_days < num_days) {
7+
result_date.addDaysUTC(1);
8+
var day_of_week = result_date.getDayOfWeekUTC();
9+
if (day_of_week != 6 && day_of_week != 7) { // Skip Saturday (6) and Sunday (7)
10+
added_days++;
11+
}
12+
}
13+
14+
return result_date.getDate();
15+
}
16+
17+
// Example usage:
18+
gs.print(add_business_days('2025-10-24 12:00:00', 5)); // Adds 5 business days

0 commit comments

Comments
 (0)