Days Count

download Days Count

of 3

Transcript of Days Count

  • 8/2/2019 Days Count

    1/3

    How to get day counts between to given dates excluding Saturday and Sunday.?

    Key for the given requirement is the date variable in ABAP itself. Actually date variable in ABAP is the count of

    days from 01.01.0001 to given date internally.

    So if we pass the date variable value to an integer variable and display it, it will display the day count for

    01.01.0001 to the given date.

    e.g: DATA: lv_date TYPE d VALUE '00010201',

    lv_count TYPE i.

    lv_count = lv_date.

    WRITE:/ lv_count.

    The output is 31.

    Now to how to get the day of a date?

    The answer again lies in the date variable. If we get the modulus of 7 for the date variable it 'll give the day as

    2 -> Monday

    3 -> Tuesday

    4 -> Wednesday

    5 -> Thursday

    6 -> Friday

    0 -> Saturday

    1 -> Sunday

    E.g. DATA: lv_date TYPE d VALUE '20120102',

    lv_count TYPE i.

    lv_count = lv_date MOD 7.

    WRITE:/ lv_count.

    The Output is 2 i.e 01.01.2012 is a Monday.

    Now the following Function Module can be used for getting the day count excluding Saturday and Sunday.

    FUNCTION ZGET_DAYS_COUNT .

    *"----------------------------------------------------------------------

    *"*"Local Interface:

    *" IMPORTING

    *" REFERENCE(IV_DATE_FROM) TYPE BEGDA

    *" REFERENCE(IV_DATE_TO) TYPE ENDDA OPTIONAL

    *" EXPORTING

    *" REFERENCE(EV_NO_DAYS) TYPE INT4

    *"----------------------------------------------------------------------

    * Technical Consultant : Rakesh M Mondal

    * Date : 06.09.2010

    * Description : Get Days Count between to dates excluding

    * Saturday and Sunday

    *"---------------------------------------------------------------------*

    *Local Data declarations

    DATA: lv_date_to TYPE endda,

    lv_total_days TYPE i,

    lv_day_from TYPE p,

  • 8/2/2019 Days Count

    2/3

    lv_day_to TYPE p,

    lv_week TYPE i,

    lv_days TYPE i,

    lv_fw_count TYPE i.

    *~If To date is not given it 'll curent system date~*IF iv_date_to IS INITIAL.

    lv_date_to = sy-datum.

    ELSE.

    lv_date_to = iv_date_to.

    ENDIF.

    *Calculatin the total no of days between the given dates including the to date.

    lv_total_days = lv_date_to - iv_date_from + 1 .

    *~Determining the day of from date~*

    PERFORM get_day USING iv_date_from CHANGING lv_day_from.

    *~Determining the day of to date~*

    PERFORM get_day USING lv_date_to CHANGING lv_day_to.

    *if from date is not a monday then

    *deduct the no of days till first monday

    *and calculate the Saturday and sunday for that week.

    IF lv_day_from NE 1.

    lv_days = lv_total_days - ( ( 7 - lv_day_from ) + 1 ).

    IF lv_day_from GT 6.

    " If the day is Sunday then LV_FW_COUNT will be 1

    lv_fw_count = 1.

    ELSE.

    * if it is Monday to Satuday then the LV_FW_COUNT will be 2.

    lv_fw_count = 2.

    ENDIF.

    ELSE.

    *if From date is a Monday then just pass the total no of days between*From date to TO DATE

    lv_days = lv_total_days.

    ENDIF.

    *if the To Date is not a sunday then

    *deduct the no of days till last sunday

    IF lv_day_to NE 7.

    lv_days = lv_days - lv_day_to .

    ENDIF.

    *Calculating no of weeks for the remainig dayslv_week = lv_days / 7.

  • 8/2/2019 Days Count

    3/3

    * Calculating total no of days excluding saturday and sunday

    * = ( total no of day between the given intput ) -

    * ( twice of week i.e no of Saturday and Sunday ) -

    * ( Satuday and Sunday of the first week ).

    ev_no_days = lv_total_days - ( 2 * lv_week ) - lv_fw_count.

    ENDFUNCTION.

    *&---------------------------------------------------------------------*

    *& Form GET_DAY

    *&---------------------------------------------------------------------*

    * To get the day for the given date

    * 2 -> Monday* 3 -> Tuesday

    * 4 -> Wednesday

    * 5 -> Thursday

    * 6 -> Friday

    * 0 -> Saturday

    * 1 -> Sunday

    *----------------------------------------------------------------------*

    * -->P_Date Date

    * 1.

    p_day = p_day - 1.

    ELSE.

    p_day = p_day + 6.

    ENDIF.

    ENDFORM. " GET_DAY