Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get the 3rd Friday of every month, need to populate on change_request planned start date

Amit Kumar13
Tera Expert

Hi,
I need to populate planned start date and end date based on selecting ci, the 3rd Friday date should be auto populate
Need an urgent help.

1 ACCEPTED SOLUTION

J Siva
Kilo Patron
Kilo Patron

Hi @Amit Kumar13 
Try the below script. it'll give you the 3rd Friday of the current month. 
Modify the script as required.

var gdt = new GlideDateTime();
var year = gdt.getYear();
var month = gdt.getMonth();

var str = year + "-" + month + "-01 00:00:00";
var date = new GlideDateTime(str);

var fridayCount = 0;
while (fridayCount < 3) {
    var dayOfWeek = date.getDayOfWeek(); // 6 = Friday
    if (dayOfWeek == '5') {
        fridayCount++;
        if (fridayCount == 3) {
            break;
        }
    }
    date.addDays(1);
}

gs.print(date.getDisplayValue());

Regards,
Siva

View solution in original post

2 REPLIES 2

J Siva
Kilo Patron
Kilo Patron

Hi @Amit Kumar13 
Try the below script. it'll give you the 3rd Friday of the current month. 
Modify the script as required.

var gdt = new GlideDateTime();
var year = gdt.getYear();
var month = gdt.getMonth();

var str = year + "-" + month + "-01 00:00:00";
var date = new GlideDateTime(str);

var fridayCount = 0;
while (fridayCount < 3) {
    var dayOfWeek = date.getDayOfWeek(); // 6 = Friday
    if (dayOfWeek == '5') {
        fridayCount++;
        if (fridayCount == 3) {
            break;
        }
    }
    date.addDays(1);
}

gs.print(date.getDisplayValue());

Regards,
Siva

yella123
Tera Expert

hi @Amit Kumar13 
Create an onChange client script on the cmdb_ci field.
In the script, calculate the 3rd Friday of the current or next month.
Set the Planned Start and End Date fields to that date using g_form.setValue().