- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2021 04:36 AM
Can anyone please advise about how to calculate a certain number of BUSINESS days from today's date?
USE CASE:
Phone Decommissioning: I need to use a 'date' variable where user needs to add a date that is not earlier than 10 business days from today.
I would need to create a script include function that I can then pull in a GlideAjax.
At the moment, I am using this utility function to calculate the calendar days and it works correctly, if wanting to calculate ONLY calendar days, not BUSINESS days:
isSelectedDateAfterCertainDays: function() {
if (JSUtil.nil(this.getParameter('sysparm_selectedDate')) || JSUtil.nil(this.getParameter('sysparm_days'))) {
return false;
}
var gdt = new GlideDateTime(gs.nowDateTime());
var selectedDate = this.getParameter('sysparm_selectedDate');
var ed = new GlideDateTime(gdt);
ed.addDays(this.getParameter('sysparm_days'));
var diff = gs.dateDiff(ed, selectedDate, true);
if (diff > 0)
return 'true';
else
return 'false';
},
Went through numerous articles here, even the long-running helpful article by MB found here, but honestly, being a coding beginner, I would very much appreciate help with creating a script include function that would calculate the date EXCLUDING the weekends....
Can anyone help, please?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2021 04:23 AM
i updated the code with glide schedule. Make sure to create a valid scheduled cmn_scheduled table for weekdays and pass that sys_id into glideschedule
isSelectedDateAfterCertainDays: function() {
if (JSUtil.nil(this.getParameter('sysparm_selectedDate')) || JSUtil.nil(this.getParameter('sysparm_days'))) {
return false;
}
var selectedDate = this.getParameter('sysparm_selectedDate');
var gdt = new GlideDateTime(selectedDate);
var ed = new GlideDateTime();
var days =this.getParameter('sysparm_days')
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
var schedule = new GlideSchedule('38fa64edc0a8016400f4a5724b0434b8'); //sys_id of schedule cmn_schedule table
var end = schedule.add(ed, dur);
var diff = end.before(gdt);
return diff;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2021 06:22 AM
You can add following code to your script, also create a relevant schedule for weekdays
var startDate = new GlideDateTime();
var days = 10;
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
var schedule = new GlideSchedule('38fa64edc0a8016400f4a5724b0434b8'); //sys_id of schedule cmn_schedule table
var end = schedule.add(startDate, dur);
gs.info(end);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2021 02:39 AM
Hello
Thank you. I cannot, however, set the number of days in a variable, as this function/utility in the script include will need to be reusable for future catalog items with different 'business day numbers'.
Can you check my reply to
It still only accounts calendar days, not business days...
Thank you so much in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2021 04:23 AM
i updated the code with glide schedule. Make sure to create a valid scheduled cmn_scheduled table for weekdays and pass that sys_id into glideschedule
isSelectedDateAfterCertainDays: function() {
if (JSUtil.nil(this.getParameter('sysparm_selectedDate')) || JSUtil.nil(this.getParameter('sysparm_days'))) {
return false;
}
var selectedDate = this.getParameter('sysparm_selectedDate');
var gdt = new GlideDateTime(selectedDate);
var ed = new GlideDateTime();
var days =this.getParameter('sysparm_days')
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
var schedule = new GlideSchedule('38fa64edc0a8016400f4a5724b0434b8'); //sys_id of schedule cmn_schedule table
var end = schedule.add(ed, dur);
var diff = end.before(gdt);
return diff;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2021 11:22 PM
Hi
Thank you, this works amazingly!
Big kudos to you 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2021 06:18 AM
Hi,
Can you please try the below logic if it helps. You just have to pass the sys_id of the weekday schedule.
isSelectedDateAfterCertainDays: function() {
if (JSUtil.nil(this.getParameter('sysparm_selectedDate')) || JSUtil.nil(this.getParameter('sysparm_days'))) {
return false;
}
var gdt = new GlideDateTime(gs.nowDateTime());
var selectedDate = this.getParameter('sysparm_selectedDate');
var schedule=new GlideSchedule();
schedule.load('ADD SYSID OF WEEKDAY SCHEDULE');
var duration=schedule.duration(gdt,selectedDate);
var diff=duration.getNumericValue(); //Get answer in ms
if (diff > 10*24*60*60*1000)
return 'true';
else
return 'false';
},