How to get Last Business Day of Month

Supriya25
Tera Guru

Hi all,

I want to send report on Last business day of Month, kindly help me how to fix this issue.

 

 

var sched = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); // Check Business day, Exculde Holidays and Weekends //

var d = new GlideDateTime('2023-04-12');
var total_days = d.getDaysInMonth();
var abc = false;
var last_business_day = '';
d.setDayOfMonth(total_days);
while(!abc){
  abc = sched.isInSchedule(d);
   if(!abc){
           d.addDays(-1);
}
}

var business_day = new GlideDateTime(d).getDate();
gs.info('business_day : '+business_day);

 

 

Here I have taken 'April 2023' month , as per above script I'm getting Last working day is 'April 29th' , but April 29th is Saturday.

 

Please help me how to get right Last business Day of Month. what is wrong in my code.?

 

 

1 ACCEPTED SOLUTION

@Supriya25 Use this updated code for both current date and future date testing.

 

//var todayDate = new GlideDate+" 10:00:00";
var todayDate = "2023-09-24 10:00:00";
var startDate = new GlideDateTime(todayDate);
var dayOfMonth = startDate.getDayOfMonthUTC();
var monthOfYear = startDate.getMonthUTC();

var days = 31 - dayOfMonth;

loop:while(true){
    if(days == 0){
        break loop;
    }
    var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
    var schedule = new GlideSchedule();
    var end = schedule.add(startDate, dur);
    var newMonthOfYear = end.getMonthUTC();

    if(monthOfYear == newMonthOfYear){
    var schedule1 = new GlideSchedule("08fcd0830a0a0b2600079f56b1adb9ae");
    var date = new GlideDateTime();
    date.setDisplayValue(end);
    if(schedule1.isInSchedule(date)){
        gs.info(end);
        break loop;
    }else{
        days--;
    }
    }else{
        days--;
    }
}
 
Please mark all the answers on this questions as correct answers
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

16 REPLIES 16

@Supriya25 need to check your code I did not check that.

 

But if my code worked for you can you please mark it as correct answer 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

How to test with another month of date for Testing purpose

@Supriya25 Use this updated code for both current date and future date testing.

 

//var todayDate = new GlideDate+" 10:00:00";
var todayDate = "2023-09-24 10:00:00";
var startDate = new GlideDateTime(todayDate);
var dayOfMonth = startDate.getDayOfMonthUTC();
var monthOfYear = startDate.getMonthUTC();

var days = 31 - dayOfMonth;

loop:while(true){
    if(days == 0){
        break loop;
    }
    var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
    var schedule = new GlideSchedule();
    var end = schedule.add(startDate, dur);
    var newMonthOfYear = end.getMonthUTC();

    if(monthOfYear == newMonthOfYear){
    var schedule1 = new GlideSchedule("08fcd0830a0a0b2600079f56b1adb9ae");
    var date = new GlideDateTime();
    date.setDisplayValue(end);
    if(schedule1.isInSchedule(date)){
        gs.info(end);
        break loop;
    }else{
        days--;
    }
    }else{
        days--;
    }
}
 
Please mark all the answers on this questions as correct answers
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

@Supriya25 did you try this?

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

I have replied the updated code for testing with any date. Please check that and mark all answers as correct answers if those helped you

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023