How to get Last day of Month ( exclude sat, sun)

Sironi
Kilo Sage

Hi All,

 

Please help me to get last Calendar day of Month , only exclude Sat's , sun's. 

 

 

 

 

I tried below script but I don't know why I'm not getting Info logs ? 

and

when I ran the code in background I'm getting right result ( True / False ).

 

when I execute job manually email triggering every time irrespective of True Or False Result, that should not be happened .

 

 

function isLastWorkingDay(gdt) {

    var today = gdt.getDayOfWeekLocalTime();
    var thisMonth = gdt.getMonthLocalTime();

    switch (today) {
        // Saturday and Sunday are NOT the last working day of the month
        case 6:
        case 7:
            return false;

           // If it's a Friday, add 3 and see if the month changes.
        case 5:
            var nextMonday = gdt;
            nextMonday.addDaysLocalTime(3);
            var nextMondayMonth = nextMonday.getMonthLocalTime();
            return (nextMondayMonth != thisMonth);

            // If it's a Mon-Thu add one and see if the month changes
        default:
            var nextDay = gdt;
            nextDay.addDaysLocalTime(1);
            var nextDayMonth = nextDay.getMonthLocalTime();
            return (nextDayMonth != thisMonth);

    }
}

var now = new GlideDateTime();
gs.info('Last working day :'+now);
var lwd = isLastWorkingDay(now);
gs.info("Last working day= " + lwd);

var answer = false;
if (lwd) {
    answer = true;
}
answer;

 

Sironi_0-1686074799022.png

 

 

Sironi_1-1686074799011.png

 

 

 

 

 

4 REPLIES 4

wndydngy
Tera Expert

Hi,

 

You might wanna check this article https://www.servicenow.com/community/developer-forum/how-to-get-last-business-day-of-month/td-p/2576...

 

Let me know if this helps. Thanks!

Amit Gujarathi
Giga Sage
Giga Sage

HI @Sironi ,
I trust you are doing great.
To retrieve the last calendar day of the month excluding Saturdays and Sundays, you can use the following JavaScript code in a ServiceNow server-side script:

// Get the current date
var currentDate = new GlideDateTime();

// Set the date to the first day of the next month
currentDate.addMonths(1);
currentDate.setDayOfMonth(1);

// Subtract one day to get the last day of the current month
currentDate.addDays(-1);

// Loop until a non-Saturday and non-Sunday is found
while (currentDate.getDayOfWeek() === 6 || currentDate.getDayOfWeek() === 0) {
  currentDate.addDays(-1);
}

// The variable 'currentDate' now holds the last calendar day of the month, excluding Saturdays and Sundays
var lastDayOfMonth = currentDate.getLocalDate();

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi,

 

Thanks for your reply.

I checked with your code but wrong result coming...... kindly check it.

var currentDate = new GlideDateTime('2023-04-12');

// Set the date to the first day of the next month
currentDate.addMonths(1);
currentDate.setDayOfMonth(1);

// Subtract one day to get the last day of the current month
currentDate.addDays(-1);

// Loop until a non-Saturday and non-Sunday is found
while (currentDate.getDayOfWeek() === 6 || currentDate.getDayOfWeek() === 0) {
  currentDate.addDays(-1);
}

// The variable 'currentDate' now holds the last calendar day of the month, excluding Saturdays and Sundays
var lastDayOfMonth = currentDate.getLocalDate();
gs.info('lastDayOfMonth :'+lastDayOfMonth);

 

 

I have taken April month date, April month End date 30th it was sunday. if there is any sunday Or saturday we want to show earlier date means 28th.

 

 

 

 

I tried below script but I don't know why I'm not getting logs ? 

and

when I ran the code in background I'm getting right result ( True / False ).

 

when I execute job manually email triggering every time irrespective of True Or False Result.

 

 

function isLastWorkingDay(gdt) {

    var today = gdt.getDayOfWeekLocalTime();
    var thisMonth = gdt.getMonthLocalTime();

    switch (today) {
        // Saturday and Sunday are NOT the last working day of the month
        case 6:
        case 7:
            return false;

           // If it's a Friday, add 3 and see if the month changes.
        case 5:
            var nextMonday = gdt;
            nextMonday.addDaysLocalTime(3);
            var nextMondayMonth = nextMonday.getMonthLocalTime();
            return (nextMondayMonth != thisMonth);

            // If it's a Mon-Thu add one and see if the month changes
        default:
            var nextDay = gdt;
            nextDay.addDaysLocalTime(1);
            var nextDayMonth = nextDay.getMonthLocalTime();
            return (nextDayMonth != thisMonth);

    }
}

var now = new GlideDateTime();
gs.info('Last working day :'+now);
var lwd = isLastWorkingDay(now);
gs.info("Last working day= " + lwd);

var answer = false;
if (lwd) {
    answer = true;
}
answer;

 

Sironi_0-1686074200630.png

 

Sironi_1-1686074240066.png