Optoscript to find last Friday of each month

I’m a complete novice with opto22 and I was trying to help a coworker find the last Friday of each month to trigger something to run. Could someone give me any hints or point me in the right direction?

Hi! I think the methods GetDayOfWeek(), GetMonth(), and GetDay() are the tools you’re looking for. They return 1-7, 1-12, and 1-31 respectively.

I haven’t tested any of this, but how I would go about it is to start by creating a table of all the days in each month, something like this:

DaysInMonth[1] = 31;  // January
DaysInMonth[2] = 28;  // February
DaysInMonth[3] = 31;  // March
DaysInMonth[4] = 30;  // April
DaysInMonth[5] = 31;  // May
DaysInMonth[6] = 30;  // June
DaysInMonth[7] = 31;  // July
DaysInMonth[8] = 31;  // August
DaysInMonth[9] = 30;  // September
DaysInMonth[10] = 31; // October
DaysInMonth[11] = 30; // November
DaysInMonth[12] = 31; // December

This only needs to be done once, so I would put it in it’s own block. After that it’s a matter of checking to see if it’s Friday, if it is then check if there’s less than a week until the end of the month. To get “days remaining” just reference the table using the current month number and subtract off the current date.

if (GetDayOfWeek() == 5) then
  if ((DaysInMonth[GetMonth()] - GetDay()) < 7) then
    // trigger that thing to run
  endif
endif

I think the next leap year where February 29th is a Friday isn’t until 2036, at which point it will give (28 - 29) = -1, which is still < 7 so this shouldn’t break, but I can’t say for sure…

How exactly you want to do the repetition, whether though a loop, delay block, time check, or something of that sort will depend on your application. Here’s a little chart I threw together to demonstrate what it might look like:

Again, this is sort of a throw together, it has not been tested thoroughly (or, uh, at all) but I hope it helps!

Thank you so much! I really appreciate this!

On this part

if (GetDayOfWeek() == 5) then
if ((DaysInMonth[GetMonth()] - GetDay()) < 7) then
// trigger that thing to run
endif
endif

It seems to fail on the DaysInMonth portion. If I take that part out it compiles with no errors.

Did you create the DaysInMonth int32 numeric table? Make sure it has a length of 13.

Exactly what Philip said, but with a small extra comment, keep in mind that tables will be created from index zero, but the days in month returns numbers from 1 to 12.
That’s why Philip said to make sure the length is 13, we don’t use index zero - There is no month zero.

Also if you could drop a reply here to let us all know how you get along with the code, if it works out for you and if you tweak it in anyway…That sort of thing… The forums are helpful for lots of people following along… We all want to share our success stories.

That was it. I was able to compile everything without errors late Friday night. I will post back as soon as we test it this week. I really appreciate all the help from you guys.

Hi ClintN,

Did you have any luck with this? I need to do something similar and record on the last day of each month just before midnight.

Cheers
J

Yes Torchard pretty much gave us everything we needed after a few tweaks.