Schedule job to run last working day of month
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2017 05:36 AM
Hi Folks,
I am working on a requirement where I need to run a schedule Report on last working day of the month.If anyone has implemented some same requirement in there project or came across around the logic for running report on last working day please help me.Thanks in advance.
I am testing with some logic for now as below:
var ans;
var td= new GlideDateTime("2018-02-29 12:00:00");
gs.print('todays date '+td);
var ndm=td.getDaysInMonthLocalTime();
gs.print('number of days in current month '+ndm);
else if(ndm==31)
{ if(td.getDayOfMonthLocalTime()==29)
{
if(td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('31/29');
}
}
if(td.getDayOfMonthLocalTime()==30)
{
if(td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('31/30');
}
}
else if(td.getDayOfMonthLocalTime()==31)
{
if(td.getDayOfWeekLocalTime()==1||td.getDayOfWeekLocalTime()==2||td.getDayOfWeekLocalTime()==3||td.getDayOfWeekLocalTime()==4||td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('31/31');
}
}
}
else if(ndm==30)
{ if(td.getDayOfMonthLocalTime()==28)
{
if(td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('30/28');
}
}
if(td.getDayOfMonthLocalTime()==29)
{
if(td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('30/29');
}
}
else if(td.getDayOfMonthLocalTime()==30)
{
if(td.getDayOfWeekLocalTime()==1||td.getDayOfWeekLocalTime()==2||td.getDayOfWeekLocalTime()==3||td.getDayOfWeekLocalTime()==4||td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('30/30');
}
}
}
else if(ndm==28)
{ if(td.getDayOfMonthLocalTime()==26)
{
if(td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('28/26');
}
}
if(td.getDayOfMonthLocalTime()==27)
{
if(td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('28/27');
}
}
else if(td.getDayOfMonthLocalTime()==28)
{
if(td.getDayOfWeekLocalTime()==1||td.getDayOfWeekLocalTime()==2||td.getDayOfWeekLocalTime()==3||td.getDayOfWeekLocalTime()==4||td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('28/28');
}
}
}
else if(ndm==29)
{ if(td.getDayOfMonthLocalTime()==27)
{
if(td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('29/27');
}
}
if(td.getDayOfMonthLocalTime()==28)
{
if(td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('29/28');
}
}
else if(td.getDayOfMonthLocalTime()==29)
{
if(td.getDayOfWeekLocalTime()==1||td.getDayOfWeekLocalTime()==2||td.getDayOfWeekLocalTime()==3||td.getDayOfWeekLocalTime()==4||td.getDayOfWeekLocalTime()==5)
{
ans=true; gs.print('29/29');
}
}
}
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2017 05:38 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2017 12:06 PM
Here's a quick function I came up with to determine if it's the last working day of the month. It basically just adds 1 day (Monday-Thursday) to see if the month changes, or adds 3 days to Friday and checks if the month changes. If it does, then it's the last working day of the month. Sample use of the function on the last three lines>. Sorry about the grotesque indentation. Bad copy/paste on this community software.
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;
break;
// 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);
break;
// 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);
break;
}
}
var now = new GlideDateTime();
var lwd = isLastWorkingDay(now);
gs.info('Last working day=' + lwd);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 06:31 AM
Hi
The breaks show an unreachable code error, so I tried it with and without them. No luck either way.
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;
break;
// 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);
break;
// 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);
break;
}
}
var now = new GlideDateTime();
var lwd = isLastWorkingDay(now);
gs.info('Last working day=' + lwd);
Here is what the Schedule Job looks like, no script shown, but clicking Configure Job Definition shows the script.
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2017 12:29 PM
Here is the script that considers holidays as well
var sched = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); // Specify the schedule sys_id
var d = new GlideDateTime();
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 gdt = new GlideDateTime().getDate();
var business_day = new GlideDateTime(d).getDate();
var diff = gs.dateDiff(gdt, business_day, true)/86400;
diff == 0