How do I schedule a report for the last weekday before the 14th of each month?

JR42
Giga Guru

Hi Devs, I did not get a response on this in the SysAdmin forum so am posting here to see if you have any ideas.

I have a requirement to send a Scheduled Report on the last business day closest to, and before, the 14th of every month. Can anyone point me in the right direction?  I use the script before for last business day of the month, and thinking I should be able to modify this.

 

//Last Weekday of the Month
//You have to set the interval for the month and the day of the week.
// lastWeekDayOfMonth
// dayOfWeek is 0 = Sunday, 6 = Saturday
var lastWeekDayOfMonth = function (months, dayOfWeek) {
if( dayOfWeek < 0 || dayOfWeek > 6 ) {
 return false;
}
var today = new Date();
 if( today.getDay() != dayOfWeek ) {
  return false;
}
var month = today.getMonth() + 1;
 if( months.indexOf(month) == -1 ) {
  return false;
}
var  sawAnother = false;
 var td = new Date(today.getTime() + 86400000);
    while( td.getMonth() == today.getMonth() && !sawAnother ) {
  sawAnother = td.getDay() == dayOfWeek;
        td = new Date(td.getTime() + 86400000);
}
 return !sawAnother;
};
lastWeekDayOfMonth([1,2,3,4,5,6,7,8,9,10,11,12], 1,2,3,4,5);

 

1 ACCEPTED SOLUTION

Mike_R
Kilo Patron
Kilo Patron

I took a shot at this. It's not the prettiest code and maybe there's a more efficient way, but this should work

 

var answer = false;
var dayToSend = 14;


var currentDate = new GlideDateTime();
var currentMonth = currentDate.getMonthUTC();
var currentYear = currentDate.getYearUTC();
var currentDayofMonth = currentDate.getDayOfMonthUTC();

var fourteenthOfMonth = new GlideDateTime(currentYear + "-" + currentMonth + "-" + dayToSend);
var dayofWeek = fourteenthOfMonth.getDayOfWeekUTC();


if (dayofWeek == 6) { // If the 14th is a Saturday, send on the Friday before

    dayToSend = 13;

}

if (dayofWeek == 0) { // If the 14th is a Sunday, send on the Friday before

    dayToSend = 12;

}



if (currentDayofMonth == dayToSend) {
    answer = true;
} else {
    answer = false;
}

 

 

 

View solution in original post

2 REPLIES 2

Mike_R
Kilo Patron
Kilo Patron

I took a shot at this. It's not the prettiest code and maybe there's a more efficient way, but this should work

 

var answer = false;
var dayToSend = 14;


var currentDate = new GlideDateTime();
var currentMonth = currentDate.getMonthUTC();
var currentYear = currentDate.getYearUTC();
var currentDayofMonth = currentDate.getDayOfMonthUTC();

var fourteenthOfMonth = new GlideDateTime(currentYear + "-" + currentMonth + "-" + dayToSend);
var dayofWeek = fourteenthOfMonth.getDayOfWeekUTC();


if (dayofWeek == 6) { // If the 14th is a Saturday, send on the Friday before

    dayToSend = 13;

}

if (dayofWeek == 0) { // If the 14th is a Sunday, send on the Friday before

    dayToSend = 12;

}



if (currentDayofMonth == dayToSend) {
    answer = true;
} else {
    answer = false;
}

 

 

 

Tony Chatfield1
Kilo Patron

another option, although it doesn't allow for scheduled public holidays

//var gdt = new GlideDateTime('2022-01-13 18:30:00');
//var gdt = new GlideDateTime('2022-05-12 18:30:00');
var gdt = new GlideDateTime('2022-08-11 18:30:00');

var myDate = gdt.getDayOfMonthLocalTime();
gs.info('myDate  ' + myDate);

//we only need to check further is the dates falls within the target range
if (myDate >= 12 && myDate <= 14) {
    var dayOfWeek = gdt.getDayOfWeekLocalTime();
    gs.info('dayOfWeek ' + dayOfWeek);
    if (myDate == 14 && (dayOfWeek >= 1 && dayOfWeek <= 5)) {
        // run script as it is a weekday
        gs.info('the 14th is a weekday');
    } else if ((myDate == 12 || myDate == 13) && dayOfWeek == 5) {
        //run script as 14th is a weekend day.
        gs.info('myDate is ' + myDate + ' and day of the week is day ' + dayOfWeek);
    }
}