How to run a Scheduled Job on last week wednesday of every month

Community Alums
Not applicable

Hi All,

I have created  a scheduled job to run on last week wednesday of every month,but its failing to run for some month

var answer = false;
var rightNow = GlideDateTime();
var dayOfWeek = rightNow.getDayOfWeekLocalTime();
var day = rightNow.getDayOfMonthLocalTime();
if (dayOfWeek == 3 && ((day >= 23 && day <= 28) || (day >= 29 && day <= 31))){
    answer = true;
}
answer;

Anyone can please help me on this.

Thanks,

1 ACCEPTED SOLUTION

Abhijit4
Mega Sage

Hi,

Your below condition

if (dayOfWeek == 3 && ((day >= 23 && day <= 28) || (day >= 29 && day <= 31))){

is equivalent to below 

if (dayOfWeek == 3 && ((day >= 23 && day <= 31)){

This will create problem when 23rd and 30th both are Wednesday and if you plan to exclude 23 or 24 then it will create problem for Feb month.

Rather you can use below script with more logical condition:

var answer = false;
var rightNow = GlideDateTime();
var totalDaysInMonths=gdt.getDaysInMonthLocalTime()
var dayOfWeek = rightNow.getDayOfWeekLocalTime();
var day = rightNow.getDayOfMonthLocalTime();
if (dayOfWeek == 3 && ((day >(totalDaysInMonths-7)){
    answer = true;
}
answer;

This might resolve your issue as well. I am assuming that your scheduled job is running daily.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Regards,
Abhijit
Community Rising Star 2022

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

3 REPLIES 3

Yousaf
Giga Sage

Hi Sangeetha,

Please refer and try the solution.

Get last weekday ("x") of month


var dayOfWeek_toLookFor = 3; // Wednesday



// Set the following to any date in the month you want to find the last


// Xday for. In this example, I'll find the last Wednesday in September


var dateInMonthToCheck = new Date(2017, 8, 1); // Note that 8 = September



// Here there be magic


if (!isNaN(dateInMonthToCheck.getTime())) {


       var lastXDay = new Date(dateInMonthToCheck.getFullYear() +


                       (dateInMonthToCheck.getMonth() === 11 ? 1 : 0),


                       (dateInMonthToCheck.getMonth() + 1) % 12);


       lastXDay.setDate(0); // Last day of previous month


       lastXDay.setDate(lastXDay.getDate() -


                       (lastXDay.getDay() + 7 - dayOfWeek_toLookFor) % 7);


       // Do stuff here with lastXDay


}

 

Mark Correct or Helpful if it helps.


***Mark Correct or Helpful if it helps.***

Abhijit4
Mega Sage

Hi,

Your below condition

if (dayOfWeek == 3 && ((day >= 23 && day <= 28) || (day >= 29 && day <= 31))){

is equivalent to below 

if (dayOfWeek == 3 && ((day >= 23 && day <= 31)){

This will create problem when 23rd and 30th both are Wednesday and if you plan to exclude 23 or 24 then it will create problem for Feb month.

Rather you can use below script with more logical condition:

var answer = false;
var rightNow = GlideDateTime();
var totalDaysInMonths=gdt.getDaysInMonthLocalTime()
var dayOfWeek = rightNow.getDayOfWeekLocalTime();
var day = rightNow.getDayOfMonthLocalTime();
if (dayOfWeek == 3 && ((day >(totalDaysInMonths-7)){
    answer = true;
}
answer;

This might resolve your issue as well. I am assuming that your scheduled job is running daily.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Regards,
Abhijit
Community Rising Star 2022

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

MrMuhammad
Giga Sage

Try this

var answer = false;
var rightNow = GlideDateTime();
var dayOfWeek = rightNow.getDayOfWeekLocalTime();
var day = rightNow.getDayOfMonthLocalTime();
if (dayOfWeek == 3 && ((day >= 23 && day <= 31))){
    answer = true;
}
gs.info(answer);
Regards,
Muhammad