Skip to Content
Author's profile photo Former Member

Custom function module to get the dates of last Friday/any day of given number of months and date

Purpose:


Purpose of this document is to develop a custom function module to get the dates of last Friday/any day of given number of months and date.


FM code:


Please maintain mentioned input parameters to the custom FM.

Capture.PNG

And also maintain the below mentioned Export parameter.

Capture.PNG

Please find the below source code to get the dates of last day/Friday of given number of months.

DATA : lv_last_date TYPE sy-datum,
lv_tmp_date TYPE sy-datum,
lv_week_day TYPE week_day.

*loop IV_MONTHS no of times to get the required dates

DO iv_months TIMES.
CLEAR : lv_last_date,lv_tmp_date.

*Use this function module to get the last day of the month

CALL FUNCTION ‘OIL_MONTH_GET_FIRST_LAST’
EXPORTING
i_date    = iv_date
IMPORTING
e_last_day = lv_last_date
EXCEPTIONS
wrong_date = 1
OTHERS    = 2.
IF sy-subrc = 0.
lv_tmp_date = lv_last_date.

DO .

*Use the FM to get Day of given Date.

CALL FUNCTION ‘DATE_TO_DAY’
EXPORTING
date    = lv_tmp_date
IMPORTING
weekday = lv_week_day.
IF lv_week_day CS iv_day+0(3).
APPEND lv_tmp_date TO et_date.
EXIT.
ELSE.
lv_tmp_date = lv_tmp_date – 1.
ENDIF.
ENDDO.
ENDIF.

*Increment month
IF iv_date+4(2) <= ’11’.
iv_date+4(2) = iv_date+4(2) + 1.
ELSE.
CLEAR iv_date+4(2).
iv_date+0(4) = iv_date+0(4) + 1.
iv_date+4(2) = iv_date+4(2) + 1.
ENDIF.
ENDDO.

ENDFUNCTION.

Results of custom FM:

            Here, I would   like to get the dates of last Friday of next 3 months (Sep, Oct and Nov) and giving input as today’s date 30.09.2016.

Test Input:

Capture.PNG

Output of FM:

Capture.PNG

Please find the dates of last Fridays for Sep, Oct and Nov in Calendar.

  Capture.PNG Capture.PNGCapture.PNG

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Kiran K
      Kiran K

      Lakshmi,

      Did you considered Leap Years also when arriving the date/s for last Friday.

      K.Kiran

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi Kiran,

      Yes , I have consider the leap years also.

      Regards,

      Lakshmi.