- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2017 01:05 PM
I have combed through the community looking for an answer to the following question but have not found one yet. I currently have a schedule job that generates monthly standard change requests a month prior to a maintenance window. I would like to populate the planned start date for each standard change with the following months maintenance window date and time. The maintenance windows take place on the second Sunday of every month. An example would be once the current maintenance window closed on November 13 the next set of standard changes would be created by a schedule job for the December maintenance window on December 11th. The planned start date in this example would be December 11th but differs every month depending on what day the second sunday falls on. Any help would be greatly appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 09:07 AM
Thanks for your help Sachin. I spent sometime on this yesterday and answered my own question. I modified the following javascript to work within ServiceNow.
Parameters:
n = 1-5 for first, second, third, fourth or fifth weekday of the month
d = full spelled out weekday (Monday-Friday)
m = Numericial value of the month (1-12)
y = Four digit representation of the year (2017)
Return Values:
returns 1-31 for the date of the queried month/year that the nth weekday falls on.
returns false if there isn't an nth weekday in the queried month/year
getNthWeekday: function(n,d,m,y) {
var targetDay, curDay=0, i=1, seekDay;
if(d=="Monday") seekDay = 1;
if(d=="Tuesday") seekDay = 2;
if(d=="Wednesday") seekDay = 3;
if(d=="Thursday") seekDay = 4;
if(d=="Friday") seekDay = 5;
if(d=="Saturday") seekDay = 6;
if(d=="Sunday") seekDay = 7;
while(curDay < n && i < 31){
targetDay = new GlideDateTime(y+"-"+m+"-"+i++ +" 12:00:00");
if(targetDay.getDayOfWeekUTC()==seekDay) curDay++;
}
if(curDay==n){
targetDay = targetDay.getDate();
return targetDay;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2017 01:12 PM
Hi Ryan,
Please check below for solutions
Planned start date of change to default to 5 days in the future
Regards,
sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2017 05:52 AM
Hi Sachin,
Yes I am aware I can add a specific number of days to the date but in my case the number of days can change depending on how many weeks there are between the second Sunday of each month. There will either be 28 or 35 days between them. Is it possible to calculate that for each month?
Example:
Nov 12 to Dec 10 = 28 Days
Dec 10 to Jan 14 = 35 Days

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2017 07:01 AM
You can create a custom script include which returns difference between each sunday of next month.
And, then you can call this script include in your scheduled job script.
Regards,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 09:07 AM
Thanks for your help Sachin. I spent sometime on this yesterday and answered my own question. I modified the following javascript to work within ServiceNow.
Parameters:
n = 1-5 for first, second, third, fourth or fifth weekday of the month
d = full spelled out weekday (Monday-Friday)
m = Numericial value of the month (1-12)
y = Four digit representation of the year (2017)
Return Values:
returns 1-31 for the date of the queried month/year that the nth weekday falls on.
returns false if there isn't an nth weekday in the queried month/year
getNthWeekday: function(n,d,m,y) {
var targetDay, curDay=0, i=1, seekDay;
if(d=="Monday") seekDay = 1;
if(d=="Tuesday") seekDay = 2;
if(d=="Wednesday") seekDay = 3;
if(d=="Thursday") seekDay = 4;
if(d=="Friday") seekDay = 5;
if(d=="Saturday") seekDay = 6;
if(d=="Sunday") seekDay = 7;
while(curDay < n && i < 31){
targetDay = new GlideDateTime(y+"-"+m+"-"+i++ +" 12:00:00");
if(targetDay.getDayOfWeekUTC()==seekDay) curDay++;
}
if(curDay==n){
targetDay = targetDay.getDate();
return targetDay;
}
}