How to find the closest schedule date to my change request planned start date

surya76
Tera Contributor

my change request planned start date : 2024-03-20 01:00:00


Script: overlapSpan Dates:   2024-04-03 01:00:00

Script: overlapSpan Dates:   2024-03-27 01:00:00 
Script: overlapSpan Dates:   2024-04-10 01:00:00 

 

To my  planned start date : 2024-03-20 01:00:00 the closest overlapSpan Date is  2024-03-27 01:00:00 

 

 

How to find this via scripting?

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@surya76 

You can use below sample script to find the closed schedule date:

 

// Define your planned start date and overlapSpan Dates
var plannedStartDate = new GlideDateTime('2024-03-20 01:00:00');
var overlapSpanDates = [
    new GlideDateTime('2024-04-03 01:00:00'),
    new GlideDateTime('2024-03-27 01:00:00'),
    new GlideDateTime('2024-04-10 01:00:00')
];

// Initialize variables to hold the closest overlapSpan Date and the time difference
var closestDate = null;
var minDifference = Number.MAX_VALUE;

// Loop through each overlapSpan Date and find the closest one
for (var i = 0; i < overlapSpanDates.length; i++) {
    var difference = Math.abs(plannedStartDate.getNumericValue() - overlapSpanDates[i].getNumericValue());
    if (difference < minDifference) {
        minDifference = difference;
        closestDate = overlapSpanDates[i];
    }
}

// Output the closest overlapSpan Date
gs.info('Closest overlapSpan Date to planned start date is: ' + closestDate);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

3 REPLIES 3

Sohail Khilji
Kilo Patron
Kilo Patron

Were do you get this dates from ?

 

Script: overlapSpan Dates:   2024-04-03 01:00:00

Script: overlapSpan Dates:   2024-03-27 01:00:00 
Script: overlapSpan Dates:   2024-04-10 01:00:00 

 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect


var ss = new GlideRecord('cmn_schedule_span');
ss.addQuery('schedule', 'sys ids of the maintenance schedules');
ss.orderBy('start_date_time');
ss.query(); // consider i have 2 maitenace schedules
while (ss.next()) {


var timeZone = "GMT";
var thisSchedule = new GlideSchedule();
thisSchedule.setTimeZone(timeZone);

gs.log("addHoliday to Schedule " + ss.name);      thisSchedule.addTimeSpan(ss);

Start from Today
var gd = new GlideDate();
var gdt = new GlideDateTime();
gdt.setNumericValue(gd.getNumericValue());

/ GDT (GlideDateTime)
// Start Datevar sd = new GlideDateTime();
sd.setValue(gdt.getDate() + " 00:00:00");
/ End Date
var ed = new GlideDateTime();
ed.setValue(gdt.getDate() + " 23:59:59");
ed.addDaysUTC(730); // Look 2 years ahead (this could be anything)
var ssd =   new GlideScheduleDateTime(sd); // Start Day
ssd.setTimeZone(timeZone);
var sed =   new GlideScheduleDateTime(ed); // End Day
sed.setTimeZone(timeZone);  
var scheduleMap = thisSchedule.getTimeMap(sd, ed, timeZone);
var span = new GlideScheduleDateTimeSpan(ssd, sed);
var thisMap = new GlideScheduleTimeMap();
thisMap.addInclude(span);
thisMap.buildMap(timeZone);
overlaps = scheduleMap.overlapsWith(thisMap, timeZone);
overlaps.buildMap(timeZone);
while (overlaps.hasNext()) {
      var overlapSpan = overlaps.next();
           gs.log("overlapSpan Dates:   " + overlapSpan.getStart() + " - " + overlapSpan.getEnd());
}
}

output:

FOR fist schedule

*** Script: overlapSpan Dates: 2024-03-27 01:00:00 - 2024-03-27 23:00:00
*** Script: overlapSpan Dates: 2024-04-03 01:00:00 - 2024-04-03 23:00:00

output for second schedule

*** Script: overlapSpan Dates: 2024-03-21 07:00:00 - 2024-03-21 21:00:00
*** Script: overlapSpan Dates: 2024-04-21 07:00:00 - 2024-04-21 21:00:00


This is how i get the schedule dates of maintenace windows


how to check which date is closest to my planned start date of change request: 2024-03-20 01:00:00

 

Maddysunil
Kilo Sage

@surya76 

You can use below sample script to find the closed schedule date:

 

// Define your planned start date and overlapSpan Dates
var plannedStartDate = new GlideDateTime('2024-03-20 01:00:00');
var overlapSpanDates = [
    new GlideDateTime('2024-04-03 01:00:00'),
    new GlideDateTime('2024-03-27 01:00:00'),
    new GlideDateTime('2024-04-10 01:00:00')
];

// Initialize variables to hold the closest overlapSpan Date and the time difference
var closestDate = null;
var minDifference = Number.MAX_VALUE;

// Loop through each overlapSpan Date and find the closest one
for (var i = 0; i < overlapSpanDates.length; i++) {
    var difference = Math.abs(plannedStartDate.getNumericValue() - overlapSpanDates[i].getNumericValue());
    if (difference < minDifference) {
        minDifference = difference;
        closestDate = overlapSpanDates[i];
    }
}

// Output the closest overlapSpan Date
gs.info('Closest overlapSpan Date to planned start date is: ' + closestDate);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks