Calculate Date in Business Days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 01:38 PM
Hello Community,
How can I calculate a target date counting business days only?
Example : User enters start date on catalog item date field - 1/24/2022
Goal : The script should calculate 10 days from the start date considering *business days only* and should populate the end date field with 2/7/2022
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 02:40 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 02:47 PM
var input = "01-24-2022";
var number = 10;
var startDate = new GlideDate();
startDate.setValue(input);
gs.print(startDate);
var days = parseInt(number); //assuming there are 8 business hours
days = days*8;
var dur = new GlideDuration(60 * 60 * 1000 * days);
var schedule = new GlideSchedule(gs.getProperty('cmn_schedule.business_days')); //put your schedule sys_id here
var end = schedule.add(startDate, dur);
gs.print(end);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2022 01:51 AM
1. Create a schedule "Weekday" that defined to be Mon-Fri. If needed, add holidays as exception.
2. Create script. Variable "strDatetime" is the start date. Variable "days" is number of days + 1.
// calculate business days between startDate and endDate
function getBusinessDays(startDate, endDate) {
var schedule = new GlideSchedule();
schedule.load('b4560c4597d1011086d3b4b3f153afc1'); // sys_id of weekday schedule
var duration = schedule.duration(startDate, endDate);
return duration.getDayPart();
}
// find date that is diffDays before startDate
function getEndDate(startDate, diffDays) {
var diffTime = 24 * 60 * 60 * 1000;
var endDate = new GlideDateTime(startDate);
endDate.subtract(diffTime * diffDays);
var gtime2 = endDate.getTime();
var eDate = new GlideDateTime(endDate.getDate() + " 00:00:00");
return eDate;
}
// find date that is diffDays business days before startDate
function findEndDate(strDatetime, days) {
var daysApart = days;
var sDate = new GlideDateTime(strDatetime);
var eDate = getEndDate(strDatetime, daysApart);
var busiDays = getBusinessDays(eDate, sDate);
var cnt = 0;
while (busiDays < days && cnt < 3) {
daysApart += days - busiDays;
eDate = getEndDate(strDatetime, daysApart);
busiDays = getBusinessDays(eDate, sDate);
cnt++;
}
return eDate;
}
var days = 11; //number of days before + 1
var strDatetime = "2022-01-24 00:00:00"; // start date
var endDate = findEndDate(strDatetime, days);
gs.info("finish end:" + endDate);
Execution result:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2022 07:30 AM
Hitoshi,
This is helpful. However, it's counting backwards. The end_date should be 10 days into the future, not the past. The calculations seem to be accurate though.