SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Get the date of the first friday of the month



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Calendar
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sat Sep 22, 2007 5:33 pm    Post subject: Get the date of the first friday of the month Reply with quote

CONSTANTS: c_date_reference TYPE d VALUE '19790101',
c_friday TYPE i VALUE 5.
Code:
DATA: first_of_month TYPE d,
      first_friday   TYPE d,
      day            TYPE i.

PARAMETERS: p_mon TYPE c LENGTH 6.

first_of_month = p_mon.
first_of_month+6(2) = '01'.

day = ( first_of_month - c_date_reference ) MOD 7.

first_friday = first_of_month + ( ( 11 - day  ) MOD 7 ).

WRITE: / first_friday DD/MM/YYYY.


Here is another sample code using a standard function module. The only interest of mine is that you do not need to know the day for 01.01.1979 in the code above.

Code:
REPORT z_find_first_day.

*----------------------------------------------------------------------*
* This program calculates the first day of the type you want :
*   - P_DAY = 1 for Monday, 2 for Tuesday,... and 7 for Sunday
*   - P_DATE is a date of the month and year you are interested in
*----------------------------------------------------------------------*
* Example :
* You search the date for the first Friday of April 2007, you enter :
*   - P_DAY = 5
*   - P_DATE must be between 1st of April 2007 and 30th of April 2007
*----------------------------------------------------------------------*
*                DATA DECLARATION
*----------------------------------------------------------------------*

DATA :
* Dates
  w_first_day_of_month  TYPE sy-datum,
  w_result_day          TYPE sy-datum,

* Day number in week
  w_day_in_week TYPE p,

* Delta
  w_delta       TYPE i.

*----------------------------------------------------------------------*
*                SELECTION-SCREEN
*----------------------------------------------------------------------*

PARAMETERS : p_day(1)  TYPE n,
             p_date    TYPE sy-datum.


*----------------------------------------------------------------------*
*                AT SELECTION-SCREEN
*----------------------------------------------------------------------*

AT SELECTION-SCREEN ON p_day.
  IF p_day > 7 OR p_day < 1.
    MESSAGE e531(0u) WITH 'You must choose a number between 1 and 7'.
  ENDIF.

*----------------------------------------------------------------------*
*                START-OF-SELECTION
*----------------------------------------------------------------------*

START-OF-SELECTION.

* Date of first day in month of P_DATE
  w_first_day_of_month = p_date.
  w_first_day_of_month+6(2) = '01'.

* Get the day number for the first day of the month of P_DATE
  CALL FUNCTION 'DAY_IN_WEEK'
       EXPORTING
            datum = w_first_day_of_month
       IMPORTING
            wotnr = w_day_in_week.

* Get the date we are looking for!
  w_delta = ( p_day - w_day_in_week ) MOD 7.
  w_result_day = w_first_day_of_month + w_delta.

* Write the result
  WRITE w_result_day.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Calendar All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.