Master the ABAP Date Calculation with ABAP Code example

ABAP Date

ABAP Date Calculation gives a very quick tools to calculate new date from an other day. It allows in ABAP to Add or Sustract Years, Months or Days.

ABAP date Add/ Substrat Years

The easiest way in ABAP Date to Add Years is to use the standard function module ADD_TIME_TO_DATE.

  DATA: lv_new_date TYPE datum.

  CALL FUNCTION 'ADD_TIME_TO_DATE'
    EXPORTING
      i_idate               = sy-datum
      i_time                = -1 " Numbre of years to be added 
      i_iprkz               = '3' " Year
    IMPORTING
      o_idate               = lv_new_date
    EXCEPTIONS
      invalid_period        = 1
      invalid_round_up_rule = 2
      internal_error        = 3
      OTHERS                = 4.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

ABAP date Add/ Substrat Months

With the use of the same function module, you can add / remove Months from an ABAP date.

The function signature is:

  • i_idate: the base SAP Date
  • i_time: the number of Year/Month/Day to be added. in Order to substract to SAP Date, just use negative i_time.
  • i_iprkz: is the Period indicator.
  DATA: lv_new_date TYPE datum.

  CALL FUNCTION 'ADD_TIME_TO_DATE'
    EXPORTING
      i_idate               = sy-datum
      i_time                = -10 " Nb Months to be added 
      i_iprkz               = '2' " Months
    IMPORTING
      o_idate               = lv_new_date
    EXCEPTIONS
      invalid_period        = 1
      invalid_round_up_rule = 2
      internal_error        = 3
      OTHERS                = 4.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

ABAP date Add/ Substrat?Weeks

This function module also allows to add or substract Days from an SAP date?ADD_TIME_TO_DATE.?

Just?et i_iprkz to ‘1’ ( 1 for Weeks).

ABAP date Add/ Substrat Days

ADD_TIME_TO_DATE can also add or substract Days to ABAP data.
you have just to leave i_iprkz?empty.

SAP/ABAP Date format

SAP Date format

  • DATE

    The DATE data type consists of year, month, and day information to represent a date value. The default format for the DATE data type is ‘YYYY-MM-DD’. YYYY represents the year, MM represents the month, and DD represents the day. The range of the date value is between 0001-01-01 and 9999-12-31.

  • TIME

    The TIME data type consists of hour, minute, and second to represent a time value. The default format for the TIME data type is ‘HH24:MI:SS’. HH24 represents the hour from 0 to 24, MI represents the minute from 0 to 59, SS represents the second from 0 to 59.

  • SECONDDATE

    The SECONDDATE data type consists of year, month, day, hour, minute and second information to represent a date with time value. The default format for the SECONDDATE data type is ‘YYYY-MM-DD HH24:MI:SS’. YYYY represents the year, MM represents the month, DD represents the day, HH24 represents hours, MI represents minutes, and SS represents seconds. The range of the date value is between 0001-01-01 00:00:01 and 9999-12-31 24:00:00.

  • TIMESTAMP

    The TIMESTAMP data type consists of date and time information. Its default format is ‘YYYY-MM-DD HH24:MI:SS.FF7’. FFn represents the fractional seconds where n indicates the number of digits in fractional part. The range of the time stamp value is between 0001-01-01 00:00:00.0000000 and 9999-12-31 23:59:59.9999999.

Period indicator DATTP

The Period Indicator defines the type of the date number ( Year, Month or day)
The Period Indicator is set on Domain DATTP. The list of value are :

  • ‘?’ Day
  • ‘1’ Week
  • ‘2’ Month
  • ‘3’ Year

It has also two conversion routines

  • CONVERSION_EXIT_PERKZ_INPUT
  • CONVERSION_EXIT_PERKZ_OUTPUT

The common value for Perior Indicator are, depending on sap logon language are:

  • D – ‘ ‘ ? – Day
  • W – ‘1’ – Week
  • M – ‘2’ – Month
  • Y – ‘3’ ? – Year

All the value can be found in SAP table ?PRDKZT for?Language-dependent texts for SLED period indicator

Reference: