ABAP – Add working days to a date using Factory Calendar

SAPThis article contains a code snippet showing how to add given number of WORKING days to a given starting date. To get the correct factory calendar, you have to search in Plant data.

Get factory calendar ID

FORM get_calendar_id USING    iv_werks       TYPE werks_d
                     CHANGING cv_calendar_id TYPE fabkl.

  SELECT SINGLE fabkl 
    INTO cv_calendar_id 
    FROM t001w 
    WHERE werks = iv_werks.
ENDFORM.

Add working days

Now we have the factory calendar ID which we use to

  1. Covert current date to factory calendar date
  2. Add given number of days (ignoring the non-working days)
  3. Convert new factory date to normal date
FORM add_working_days USING iv_num_days    TYPE i
                            iv_calendar_id TYPE fabkl
                      CHANGING cv_date     TYPE sy-datum.

DATA: lv_factorydate     TYPE scal-facdate,
      lv_new_factorydate TYPE sy-datum.

* 1. Convert date to factory date. 
  CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
    EXPORTING
      date                = cv_date "Starting date
      factory_calendar_id = iv_calendar_id "Plant factory calendar id
    IMPORTING
      factorydate         = lv_factorydate. "Factory calender date

* 2. Add [N] number of days to factory date
  ADD iv_num_days TO lv_factorydate.

* 3. Convert factory date back to actual date
  CALL FUNCTION 'FACTORYDATE_CONVERT_TO_DATE'
    EXPORTING
      factorydate         = lv_factorydate
      factory_calendar_id = iv_calendar_id
    IMPORTING
      date                = lv_new_factorydate.
* Assign the new factory date to normal date format
  cv_date = lv_new_factorydate.
ENDFORM

2 thoughts on “ABAP – Add working days to a date using Factory Calendar

Leave a Reply