Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get the End Date for the schedule

rubesh_u
Tera Expert

I am using this script. when the planned start or planned end date falls on blackout window it is not working as expected it stops at 18:00 

function getBlackoutDatesWithinRange(scheduleId, plannedStartDate, plannedEndDate) {
var timeZone = session.getTimeZoneName();
var scheduleGUID = scheduleId;

var schedule = new GlideSchedule(scheduleGUID);
schedule.setTimeZone(timeZone);

var gdt = new GlideDateTime(plannedStartDate);
var gdte = new GlideDateTime(plannedEndDate);

// gs.log(schedule.getNextOccurrence());

// Set the start to the beginning of the day (midnight)
var sd = new GlideDateTime();
sd.setValue(gdt.getDate() + " 00:00:00");

// Set the end to the end of the day (23:59:59)
var ed = new GlideDateTime();
ed.setValue(gdte.getDate() + " 23:59:59");

// Log the actual start and end times
// gs.log("Start Date: " + sd.toString());
// gs.log("End Date: " + ed.toString());

var ssd = new GlideScheduleDateTime(sd);
ssd.setTimeZone(timeZone);

var sed = new GlideScheduleDateTime(ed);
sed.setTimeZone(timeZone);

var scheduleMap = schedule.getTimeMap(sd, ed, timeZone);
// gs.log(scheduleMap);

var span = new GlideScheduleDateTimeSpan(ssd, sed);
var thisMap = new GlideScheduleTimeMap();
thisMap.addInclude(span);
thisMap.buildMap(timeZone);

var overlaps = scheduleMap.overlapsWith(thisMap, timeZone);
overlaps.buildMap(timeZone);

var dates = [];
while (overlaps.hasNext()) {
var overlapSpan = overlaps.next();

var glideStartDate = overlapSpan.getStart();
var glideEndDate = overlapSpan.getEnd();

gs.log(glideStartDate);

// var plannedTaskAPI = new SNC.PlannedTaskAPI();
// var resp = plannedTaskAPI.calculateDuration(JSON.stringify(glideStartDate), JSON.stringify(glideEndDate), scheduleGUID);

// gs.log(resp);

gs.log(gs.dateDiff(glideStartDate, glideEndDate, false));

dates.push({
startDate: overlapSpan.getStart().toString(),
endDate: overlapSpan.getEnd().toString()
});
// gs.log(overlapSpan.getDuration());
}

return JSON.stringify(dates);
}


// Test case with logging
gs.log(getBlackoutDatesWithinRange("126760263b69169053ba5c7aa5e45a01", "2025-02-12 23:59:59", "2025-02-15 00:00:00"));

*** Script: 2025-02-15 00:00:00
*** Script: 17:59:59
*** Script: [{"startDate":"2025-02-15 00:00:00","endDate":"2025-02-15 17:59:59"}]


but expected should be

*** Script: 2025-02-15 00:00:00
*** Script: 17:59:59
*** Script: [{"startDate":"2025-02-15 00:00:00","endDate":"2025-02-16 23:59:59"}]


0 REPLIES 0