Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

get first day of month ServiceNow

austin30
Tera Expert
var currentdate = new GlideDate();
//currentdate.getByFormat("MM-dd-yyyy");
var gdt = new GlideDateTime(currentdate);
var dt= gs. beginningOfThisMonth(gdt);
gs.info(dt);
2 REPLIES 2

austin30
Tera Expert
How to get Last Business Day of Month?
var todayDate = "2023-08-03 16: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--;
    }
}

If your question is how to get last business day of the current month, first set up a Schedule excluding weekends and major holidays for your locale, then try this:

// Get Last Business Day of Month
var date;
var date2;
var sched;
var numDays = 0;

sched = new GlideSchedule("b3ed40ef130913003e7e31f18144b0af"); // a M-F schedule excluding major US holidays

// Get the days in the current month
date = new GlideDateTime();
numDays = date.getDaysInMonthUTC();

// Set the date to the last day of the month
date.setDayOfMonthUTC(numDays);

// Step backward from that date to 1, until we land on a date that matches the schedule.
for(var n=numDays;n>=1;n--) {
  date2 = new GlideDateTime();
  date2.setDayOfMonthUTC(n);
  if(sched.isInSchedule(date2)) break;
}

gs.info(date2);