sapdev logo background
sapdev logo sapdev logo
Comments

Add n number of working days to date (allow result to be a non working day)




The following ABAP code adds n number of WORKING days to a particular date, but allows result to be a non working day. For example if starting day is Wednesday and you add three days to it, resultant day would be Saturday(non working day). Where as if you wanted to totally ignore non working days resultant day would be Monday (see ABAP code to totally ignore non working days).

Simply add the below ABAP FORM into you code and call it using the usual PERFORM command:

PERFORM add_working_days_resnonwork USING ld_numdays CHANGING gd_date.


**&-------------------------------------------------------------*
*&      Form  ADD_WORKING_DAYS_RESNONWORK
*&-------------------------------------------------------------*
*       Add n number of factory days(working days) to date, but
*       allow resultant date to be a non working day
*--------------------------------------------------------------*
*      <-- P_DAYS     Number of days to add
*      <-- P_PAYDATE  Starting date
*--------------------------------------------------------------*

FORM add_working_days_resnonwork USING p_days
                       CHANGING p_paydate TYPE sy-datum.

  DATA: ld_count  TYPE i.

  ld_count = p_days.

  WHILE ld_count GT 0.
    CALL FUNCTION 'DATE_CHECK_WORKINGDAY'
      EXPORTING
        date                       = p_paydate
        factory_calendar_id        = 'GB'
        message_type               = 'I'
      EXCEPTIONS
        date_after_range           = 1
        date_before_range          = 2
        date_invalid               = 3
        date_no_workingday         = 4
        factory_calendar_not_found = 5
        message_type_invalid       = 6
        OTHERS                     = 7.
    IF sy-subrc NE  4.
      p_paydate = p_paydate + 1.
      ld_count = ld_count - 1.
    ELSE.
      p_paydate = p_paydate + 1.
    ENDIF.
  ENDWHILE.

ENDFORM.                    " ADD_WORKING_DAYS_RESNONWORK



comments powered by Disqus