The CreatorCon Call for Content is officially open! Get started here.

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 the schedule is from 8 to 17 right. So if you take time out of these limits then you will not be in schedule. It means even though it's a working day, for script it's a non working day as it's non working hour of the day.

 

So to check that the day is working day or not we must take time between 8 to 17 only.

 

new GlideDateTime() gives you current time. If your script runs at 7 am then the day will be considered as non working day.

To use new GlideDateTime() you must run your script between 8 to 17.

 

We are not sure when our code runs hence I heard coded 10.

 

Hope all clear.

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

Bert_c1
Kilo Patron

Hi @Supriya25,

 

The problem with your code is that d has "00:00:00" for the time value. And that is not withing 8 - 5. So you need to get a valid start time before using "isInSchedule();

 

The following code works for your example, it may be "cleaned up"

 

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

var d = new GlideDateTime('2023-04-12 08:00:00'); // use a complete DateTime value here
var total_days = d.getDaysInMonth();
gs.info("total_days = " + total_days);

// Find start time of the schedule
var schStart = '';
var schRecord = new GlideRecord('cmn_schedule_span');
schRecord.addQuery('schedule', '08fcd0830a0a0b2600079f56b1adb9ae');
schRecord.query();
if (schRecord.next()) {
	schStart = schRecord.start_date_time.toString();
	gs.info("Found schedule start time: " + schStart + ".");
}
else {
	gs.info("Can't find schedule start time.");
//	return;
}
var startTime = schStart.substr(9,2) + ":" + schStart.substr(11,2) + ":" + schStart.substr(13,2);

var abc = false;
var last_business_day = '';
d.setDayOfMonth(total_days);
var dStr = d.toString();
dStr = dStr.substr(0,10) + " " + startTime;
d.setDisplayValue(dStr);
gs.info("d = " + d);
var i = 0;
while(!abc){
  abc = sched.isInSchedule(d);
  gs.info("abc = " + abc);
  if(!abc) {
    d.addDays(-1);
  }
  // set time to be within the schedule
  dStr = d.toString();
  dStr = dStr.substr(0,10) + " " + startTime;
  d.setDisplayValue(dStr);
  gs.info("New d = " + d);
  if (i++ > 5)
    break;
}

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

 

And the output of that in Scripts Background is:

 

*** Script: total_days = 30
*** Script: Found schedule start time: 20080707T080000.
*** Script: d = 2023-04-30 12:00:00
*** Script: abc = false
*** Script: New d = 2023-04-29 12:00:00
*** Script: abc = false
*** Script: New d = 2023-04-28 12:00:00
*** Script: abc = true
*** Script: New d = 2023-04-28 12:00:00
*** Script: business_day : 2023-04-28

 

May need adjustment for UTC as for me in EDT timezone 4 hours are being added, but the value still falls withing 8 - 5.