Search code examples
abap

How do I calculate the number of workdays between two dates?


I want to calculate the difference between two dates in ABAP.

My first attempt was to subtract the dates from each other:

days_between = date1 - date2.

This works but I was wondering if there is a way to calculate only the work days between those two dates.


Solution

  • The list of working and off days may vary from a country to another, and from a plant to another. In ABAP-based system, there are calendars already setup for countries, to define which days are working and which ones are off (weekend, holidays). You may also define your own calendar. In a Factory Calendar, each day is assigned a serial integer number (also called a "factory date"), which is +1 between two subsequent working days (off days are ignored).

    To work with these calendars, you have to use these officially-released function modules (SAP Library - Calendar Functions):

    • DATE_CONVERT_TO_FACTORYDATE : returns the serial number of a given date
    • FACTORYDATE_CONVERT_TO_DATE : converts a given serial number into a date

    To calculate the number of days between two calendar dates, you must first get the serial numbers of these two dates by using DATE_CONVERT_TO_FACTORYDATE, and subtract the two serial numbers.