ERPClimb logoERPClimb

Code examples · Modern ABAP · Beginner

Dates in ABAP: add days, find the month end and age an invoice into buckets

The problem

Finance wants to know how old open invoices are.

How we solve it

  1. 1Todaysystem date
  2. 2Agesubtract dates
  3. 3BucketCOND
  4. 4Month endnext first minus 1

The result

Each invoice is printed with its age in days and its bucket, followed by the last day of the current month.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

Finance wants to know how old open invoices are. Each invoice has a due date. The program must say how many days it is overdue and put it into a bucket: not due, 1 to 30 days, 31 to 60 days or more than 60 days. It also needs the last day of the current month for a month-end report.

2. How we design it

A date in ABAP is a text of eight characters in the form YYYYMMDD, for example 20261125, but ABAP also lets you calculate with it: subtracting two dates gives the number of days between them, and adding a number to a date moves it forward. We take today's date from a released class, subtract the due date to get the age, and a CASE puts the age into a bucket. For the month end we go to the first day of the next month and step back one day, which also works for February and leap years without a table of month lengths.

Why we decided it this way

  1. 1Calculate with the date type, not with textAdding 30 to the text 20261125 would give nonsense. With the type d, ABAP knows about months and leap years.
  2. 2Take today from CL_ABAP_CONTEXT_INFOIt returns the system date in a form that also works in ABAP Cloud, and it is the form that is allowed in ABAP Cloud.
  3. 3Month end as 'first of next month minus one day'Months have 28 to 31 days. Stepping back from the next first needs no list of month lengths.

Before you start

  • ADT and a package such as $TMP. No SAP business data is needed: the invoices are typed into the program.

3. The code, step by step

Step 1 of 4: Take today's date and some invoices

A fixed set of invoices and the date of today.

ABAP · Class · ZCL_INVOICE_AGE (main method, part 1)
" Class frame and today's date
CLASS zcl_invoice_age DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zcl_invoice_age IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    DATA(today) = cl_abap_context_info=>get_system_date( ).
    " Three example invoices
    TYPES: BEGIN OF ty_invoice,
             name TYPE c LENGTH 8,
             due  TYPE d,
           END OF ty_invoice,
           ty_invoices TYPE STANDARD TABLE OF ty_invoice WITH EMPTY KEY.
    DATA(invoices) = VALUE ty_invoices(
      ( name = 'INV-1' due = '20261130' )
      ( name = 'INV-2' due = '20261110' )
      ( name = 'INV-3' due = '20260801' ) ).
Part by part (plain text)
  1. Class frame and today's date. GET_SYSTEM_DATE of the released class CL_ABAP_CONTEXT_INFO returns the date of the system. The variable today has the type d, so it can be used in calculations.
  2. Three example invoices. Each row has an invoice name and a due date. A date literal in quotes with eight digits is read as a date. Change the dates to see all buckets for the day you run the program.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

Each invoice is printed with its age in days and its bucket, followed by the last day of the current month.

What you should see

Example output for today = 2026-11-25: INV-1 due 2026-11-30 not due INV-2 due 2026-11-10 15 days overdue 1-30 days INV-3 due 2026-08-01 116 days overdue over 60 days Last day of this month: 2026-11-30

Try it and check the result

  • Put the segments together in the order shown, activate with Ctrl+F3 and run with F9.
  • Set the due date of INV-3 to a day exactly 60 days before today: it is in the bucket 31-60 days. Make it 61 days before today and it moves to over 60 days.
  • Check the month end for February: change the program to use a fixed date such as '20240215' instead of today. The result is 2024-02-29, a leap year.

Common mistakes

  • A date field that was never filled holds 00000000, which is not a real date. Check for an initial date before you calculate.
  • Adding 30 days is not adding one month. Use the month-end method above, or a released date function, when you mean calendar months.
  • SY-DATUM is the date of the application server and can differ from the user's time zone. For dates that depend on the user, convert from a time stamp.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: ABAP 7.54+ and ABAP Cloud. Check CL_ABAP_CONTEXT_INFO on your release.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.