How to subtract days from a schedule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2024 02:20 PM - edited 02-16-2024 07:59 PM
In the RITM table we have a field called dur date
i want to subtact 3 working days from thet due date and get the new date. (Thenumber of days might varies)
Could any one help..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2024 02:56 PM
I have used something like this. It consults a schedule that defines business days. So you can also add holidays and such. So for example, running this in my environment (plugging in my schedule on row 6), it returns Friday Feb 23 because today (Feb 16) is a Friday, and it skips Sat, Sun and also Monday because it is a US holiday.
gs.print(getDateXBusinessDaysAhead(new GlideDateTime(), 4));
function getDateXBusinessDaysAhead(startDate, daysAhead) {
var inXDays = new GlideDateTime(startDate); // Start date
var xDays = daysAhead; // Number of business days ahead you want
var schedule = new GlideSchedule(<sys_id>); //Your schedule defining which days are business days
var i = 0;
var j = 0;
// Loop through days until we have looped through X days in schedule (or looped through total of 60 times, to avoid infinite loops)
while (i < xDays && j < 60) {
// Add one day
inXDays.addDays(1);
// If day is a day in schedule, increment i
if (schedule.isInSchedule(inXDays)) {
i++;
}
j++;
}
return inXDays.getDate();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2024 03:09 PM
Hi Ivanal,
I could see in your code you have added days to todays days based on a calander.
i was to have it in the reverse order. Subtract days from todays date excluding week days.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2024 10:00 AM
Hi @JPSS to subtract days you just need to add a negative number.
inXDays.addDays(-1);
If you want to use the OOTB weekday with holidays schedule, you need to change one more line
var schedule = new GlideSchedule( '090eecae0a0a0b260077e1dfa71da828' ); //Your schedule defining which days are business days
no other change is needed for this to work.
Hope this helps
--
Bala Guthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2024 08:01 AM
One thing I would add to this is if you are using a glideDate record instead of a glideDateTime record then you would need to add the additional 8-9 hours in seconds or milliseconds to the startDate in order for this to work successfully, otherwise it will continue to loop for all 60 days until the condition for j is met.