Schedule job

Pavan rishi
Tera Contributor

How to schedule job for the second Saturday of January every year?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Pavan rishi 

something like this

1) scheduled job every month, select January

2) use this script in condition field so that job runs only on 2nd saturday

3) then write your script in "Run this script" field

answer = false;
var now = new GlideDateTime();
var year = now.getYearLocalTime();
var janFirst = new GlideDateTime(year + "-01-01 00:00:00");
var dayOfWeek = janFirst.getDayOfWeekLocalTime(); // 1=Monday, 2=Tuesday, ..., 7=Sunday

// Calculate the date of the first Saturday in January
var firstSaturday = 6 - dayOfWeek + 1;
if (firstSaturday <= 0) {
    firstSaturday += 7;
}

// Calculate the date of the second Saturday
var secondSaturday = firstSaturday + 7;

// Check if today is the second Saturday of January
if (now.getMonthLocalTime() == 0 && now.getDayOfMonthLocalTime() == secondSaturday) {
answer = true;
}

AnkurBawiskar_0-1738655562934.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Pavan rishi 

something like this

1) scheduled job every month, select January

2) use this script in condition field so that job runs only on 2nd saturday

3) then write your script in "Run this script" field

answer = false;
var now = new GlideDateTime();
var year = now.getYearLocalTime();
var janFirst = new GlideDateTime(year + "-01-01 00:00:00");
var dayOfWeek = janFirst.getDayOfWeekLocalTime(); // 1=Monday, 2=Tuesday, ..., 7=Sunday

// Calculate the date of the first Saturday in January
var firstSaturday = 6 - dayOfWeek + 1;
if (firstSaturday <= 0) {
    firstSaturday += 7;
}

// Calculate the date of the second Saturday
var secondSaturday = firstSaturday + 7;

// Check if today is the second Saturday of January
if (now.getMonthLocalTime() == 0 && now.getDayOfMonthLocalTime() == secondSaturday) {
answer = true;
}

AnkurBawiskar_0-1738655562934.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Pavan rishi 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader