The solution should be broken up into two portions:
<b>Part A: Last (or first) day of previous month</b>
This bit is very easy. All you need to do is go to the beginning of date X and then subtract 1.
* Set MYDATE to the last date in the previous month mydate = sy-datum. mydate+6(2) = '01'. mydate = mydate - 1. * And to lead into part B, go to first date in prev month mydate+6(2) = '01'.
<b>Part B: Find nth instance of day X in a month</b>
The second part is not that much more difficult. The required input is a date containing the first date in the month that you want. For example, if I want to find the second Friday in July 2004, then the input date must be '20040701'.
Now to calculate the 2nd Friday in the month, we can rely on MOD 7 of any date always giving a result of zero if the date is a Saturday. It follows that MOD 7 of any date gives a result of six if the date is a Friday:
day_of_week = indate MOD 7. " day_of_week is integer soughtdate = indate + 13 - day_of_week.
<b>A more flexible solution</b>
This could be made more flexible to find any particular date by not hardcoding 13, which was derived by formula:
offset = ( X * 7 ) - ( 7 - Y ) 13 = ( 2 * 7 ) - ( 7 - 6 )
Where, X is the xth instance of the day to find, in our case X=2 for the second instance of the day.
Y is the modulus (MOD 7) for a day, that is 0 for Saturday, 2 for Monday ... 6 for Friday.
Cheers,
Scott
Add comment