How to get second tuesday of every month for scheduling the report in servicenow

Chaitanya Redd1
Tera Guru

Hello Team,

Can anyone help me on the below script. I have to create schedule a report for every month second Tuesday and second Wednesday but it's not working can anyone help me.

var prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];
    var result;

var gdt = '2019-12-10 00:00:19';
gs.print(gdt);
 
    var month = gdt.getMonthLocalTime();
    var day = gdt.getDayOfWeek();
    var week = gdt.getDayOfMonthLocalTime();
 
    var checkWeek = prefixes[0 | week / 7];
 
   if (day == 2 && (gdt.getDate() >7 && gdt.getDate()<=14)) {
        result = true;
    } else {
        result = false;
    }
    gs.print(result);
1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

Try this code. here are the changes made and its working in bg script.

1. the date/time has to be converted to glidedatetime

2. getDate() returns complete date not days. so corrected that.

var dt = '2019-12-10 00:00:19';
var gdt = new GlideDateTime(dt);
gs.print(gdt);
var day = gdt.getDayOfWeek();
var days = gdt.getDayOfMonthLocalTime();
gs.print(day); 
gs.print(days); 
   if (day == 2 && ((days > 7  && days <=14) || (days >21 && days <=28))) {
        result = true;
    } else {
        result = false;
    }
gs.print(result);

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

15 REPLIES 15

If i understand correctly then try this:

(function() {

    var today = new GlideDateTime('2020-05-01');
	
	var dateObj = this.getDayOfMonth(today);

	gs.print('Monday: ' + dateObj.first_monday.getDate().getValue() + '\nTuesday: ' +
			dateObj.first_tuesday.getDate().getValue() + '\nWednesday: ' +
			dateObj.first_wednesday.getDate().getValue());
	
	//Validates first monday, tuesday and wednesday with today
    var result = dateObj.first_monday.getDate().getValue() == today.getDate().getValue() ||
        dateObj.first_tuesday.getDate().getValue() == today.getDate().getValue() ||
        dateObj.first_wednesday.getDate().getValue() == today.getDate().getValue();

    gs.print(result);

})();

function getDayOfMonth(today) {

	//Our return object containing first monday, tuesday and wednesday
    var returnObj = {};
	
	//1st of this month
    var first_of_this_month = GlideDateTime(today.getValue());
    first_of_this_month.setDayOfMonthUTC(1);

	//Looping as long as we do not have all 3 dates
    while ((returnObj.first_monday == undefined || returnObj.first_tuesday == undefined || returnObj.first_wednesday == undefined)) {

		//Checking day of week
        switch (first_of_this_month.getDayOfWeekUTC()) {
				
			//If its a monday then we add the date to our monday attribute and add another day to our date	
            case 1:
                returnObj.first_monday = new GlideDateTime(first_of_this_month);
				first_of_this_month.addDaysUTC(1);
                break;
				
			//If its a tuesday then we add the date to our tuesday attribute and add another day to our date	
            case 2:
                returnObj.first_tuesday = new GlideDateTime(first_of_this_month);
				first_of_this_month.addDaysUTC(1);
                break;
				
			//If its a wednesday then we add the date to our wednesday attribute and add another day to our date	
			case 3:
				returnObj.first_wednesday = new GlideDateTime(first_of_this_month);
				first_of_this_month.addDaysUTC(1);
				break;
				
			//If its not monday, tuesday or wednesday then we add 1 day to our date	
            default:
				first_of_this_month.addDaysUTC(1);
				break;
        }
    }
	
	//Return object containing 1st monday, tuesday and wednesday
    return returnObj;
}