 Former Member

# Is there any better way to do it?

( is there any other better logic)

************************************************

• aim: To find out the date of last month's 2nd friday

• num_curr_week get current week from today ( scal-DATE_GET_WEEK)

• Compute number of weeks this month num_weeks_this_month ( till date)

• compute week number for the 2nd week of last month. num_2week_last_month = num_curr_week - num_weeks_this_month - 2

• determine the first day for a week ( scal-DATE_COMPUTE_DAY)

• by defualt the date is 8th of last month ( for the first day of the 2nd week of last month)

• check if that is friday. SCAL-DATE_COMPUTE_DAY

• if not add one to the date , 9th and check if that is friday

• go on doing like that for 7 times

• when it is the friday then return the date

10|10000 characters needed characters exceeded

• Former Member
Posted on Jul 28, 2004 at 01:57 PM

Compute first day of month (eg 20040601)

Get Day Number (( 20040601 - 19000101 ) Mod 7 )+ 1.

If it is 5 it is a Friday, add 7 to get second friday

Else Add the number to make it 5 and then add 7.

10|10000 characters needed characters exceeded
• Former Member
Posted on Jul 28, 2004 at 02:10 PM

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

• 