diff --git a/Core ServiceNow APIs/GlideDateTime/Calculate Due date using user defined schedules/README.md b/Core ServiceNow APIs/GlideDateTime/Calculate Due date using user defined schedules/README.md new file mode 100644 index 0000000000..81aa7d6440 --- /dev/null +++ b/Core ServiceNow APIs/GlideDateTime/Calculate Due date using user defined schedules/README.md @@ -0,0 +1,14 @@ +**Description:** +This Script Include calculates a future due date by adding a specified number of business days to a given start date, based on a defined schedule. +This can be used anywhere within the server side scripts like fix scripts, background scripts, UI Action (server script). + +**Pre-requisite:** +A schedule record with valid schedule entries should be created in the cmn_schedule table + +**Sample:** +var daysToAdd = 4; // No of days need to be added +var script = new CaclculateDueDate().calculateDueDate(new GlideDateTime(),daysToAdd); // Passing the current date and daysToAdd value to script include +gs.print(script); + +**Output:** +*** Script: 2025-10-09 11:23:34 diff --git a/Core ServiceNow APIs/GlideDateTime/Calculate Due date using user defined schedules/script.js b/Core ServiceNow APIs/GlideDateTime/Calculate Due date using user defined schedules/script.js new file mode 100644 index 0000000000..99039b6298 --- /dev/null +++ b/Core ServiceNow APIs/GlideDateTime/Calculate Due date using user defined schedules/script.js @@ -0,0 +1,26 @@ +var CaclculateDueDate = Class.create(); +CaclculateDueDate.prototype = { + initialize: function() {}, + + calculateDueDate: function(date, days_to_add) { + var checkDate = new GlideDateTime(date); + var daysToAdd = days_to_add; + + + while (daysToAdd > 0) { + var sched = new GlideSchedule('c798c1dfc3907e1091ea5242b40131c8'); // Schedule record SYS_ID. Currently 9-5 Weekdays and Indian Public Holiday(excluded) schedule has been used + checkDate.addDaysLocalTime(1); + if (sched.isInSchedule(checkDate)) { + daysToAdd--; + + } else { + continue; + + } + } + + return checkDate.getValue(); + }, + + type: 'CaclculateDueDate' +};