schmelto
schmelto

Reputation: 425

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.

Upvotes: 2

Views: 9444

Answers (3)

Mike
Mike

Reputation: 14586

List of working days is a year or/and country specific data, sometimes it might even differ on a base of a country's region. I would check the CLDR-data, perhaps they provide permanently updated data.

Anyway, I would not advice calculating the date difference with a plain subtract operation, it might be an error prone approach and might lack an edge cases treatment. Instead, it's better to use a dedicated function module (FM) or a class, depending on your codebase guidelines:

  • HR_HK_DIFF_BT_2_DATES
  • LTRM_TIME_GAP_CALC
  • CL_RECA_DATE=>GET_DATE_DIFF( )

The latter is a very powerful class, which contains a lot of useful methods for working with dates.

Upvotes: 1

Sandra Rossi
Sandra Rossi

Reputation: 13629

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.

Upvotes: 4

Dirk Trilsbeek
Dirk Trilsbeek

Reputation: 6023

you can use an existing function module like RKE_SELECT_FACTDAYS_FOR_PERIOD to calculate the number of working days in a date range. You will need to tell the function module which calendar to use, as this depends on the location (e.g. bank holidays for different countries etc.).

Upvotes: 2

Related Questions