How to get Last day of Month ( exclude sat, sun)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2023 01:36 PM - edited 06-06-2023 11:07 AM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2023 10:38 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 08:21 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 10:30 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 11:03 AM
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;