Every plant in SAP can have its own calendar that defines plant working days. This calendar is used by planning functions to determine whether a particular day is working one or not. You can see the effects if, for example, you try to create a delivery schedule on Sunday, and the system says that actually the next working day is another one.
Very ofter we have to account for working days and exclude weekends and holidays in our ABAP. To do that, SAP provides two functions modules that are used to convert between the “normal” date and the enumerated “factory day”. The factory day is a number assigned to each working day in plant calendar. The functions are FACTORYDATE_CONVERT_TO_DATE and DATE_CONVERT_TO_FACTORYDATE and they are documented directly in SAP, in SAP standalone help (BC Extended Applications Function Library) and, of course, at se37.com.
The typical use of those functions is to add a number of days to a given date, skipping holidays and weekends. This is often asked on SAP-related forums and is one of the subjects in SAPfans ABAP FAQ.
I use those functions also for simple checks whether a day is working or not, and to move a date to a working day (forwards or backwards). Below are forms that I use. The factory calendar (FABKL) can be read directly from the table T001W. The parameter pf_correct should be ‘+’ to move forward or ‘-‘ to move backwards.
form move_to_working using pf_fabkl type fabkl pf_correct type cind changing pf_date type datum. data: lf_newdate type datum. check not pf_fabkl is initial. call function 'DATE_CONVERT_TO_FACTORYDATE' exporting correct_option = pf_correct date = pf_date factory_calendar_id = pf_fabkl importing date = lf_newdate exceptions calendar_buffer_not_loadable = 1 correct_option_invalid = 2 date_after_range = 3 date_before_range = 4 date_invalid = 5 factory_calendar_not_found = 6 others = 7. if sy-subrc = 0. pf_date = lf_newdate. endif. endform. form is_working_day using pi_werks like pbim-werks pi_date like sy-datum pi_fabkl type fabkl changing po_working like space. data: w_work like SCAL-INDICATOR, w_fabkl like t001w-fabkl. clear po_working. call function 'DATE_CONVERT_TO_FACTORYDATE' EXPORTING date = pi_date factory_calendar_id = pi_fabkl IMPORTING workingday_indicator = w_work EXCEPTIONS calendar_buffer_not_loadable = 1 correct_option_invalid = 2 date_after_range = 3 date_before_range = 4 date_invalid = 5 factory_calendar_not_found = 6 others = 7. if sy-subrc = 0 and w_work is initial. po_working = c_x. endif. endform.