SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow

Add Year, Month or Day to Date Variable in ABAP using RP_CALC_DATE_IN_INTERVAL

In ABAP using RP_CALC_DATE_IN_INTERVAL function module, developers can calculate new date value by adding a time period of time expressed as years, months or days to a given input date parameter. Of course, ABAP developers can also substract date period to find a previous date just like adding to calculate later date values.

In this ABAP tutorial, I'll share sample ABAP codes using RP_CALC_DATE_IN_INTERVAL demonstrating how to calculate date by adding to current date in following sections. RP_CALC_DATE_IN_INTERVAL ABAP function module is very similar to SQL Server DateAdd() function.

Below ABAP code block calculates new date variable by substracting or adding years, months and days to a given date parameter. After new date is calculated using ABAP function module RP_CALC_DATE_IN_INTERVAL, I use WRITE function to display input and output date variables on the screen.
The first variable is the input date value which is today, the current date.
And the second date is calculated using RP_CALC_DATE_IN_INTERVAL function module in ABAP codes.

data LV_DATE type DATS.

** Add/Substract years to date
call function 'RP_CALC_DATE_IN_INTERVAL'
 exporting
  DATE = SY-DATUM
  DAYS = 0
  MONTHS = 0
  SIGNUM = '-' " to calculate previous date
  YEARS = 1
 importing
  CALC_DATE = LV_DATE.

WRITE: 'Today', SY-DATUM, /.
WRITE: 'Previous year', LV_DATE, /.

** Add/Substract months to date
call function 'RP_CALC_DATE_IN_INTERVAL'
 exporting
  DATE = SY-DATUM
  DAYS = 0
  MONTHS = 1
  SIGNUM = '+' " to calculate following date
  YEARS = 0
 importing
  CALC_DATE = LV_DATE.

WRITE: 'Today', SY-DATUM, /.
WRITE: 'Next month', LV_DATE, /.

** Add/Substract days to date
call function 'RP_CALC_DATE_IN_INTERVAL'
 exporting
  DATE = SY-DATUM
  DAYS = 7
  MONTHS = 1
  SIGNUM = '+' " to calculate next dates
  YEARS = 2
 importing
  CALC_DATE = LV_DATE.

WRITE: 'Today', SY-DATUM, /.
WRITE: 'Add 2 Years, 1 Month and 7 Days (or 1 week)', LV_DATE, /.
Code

And the output of above ABAP program where source codes are shared is as follows:

calculate date by adding to current date using ABAP function

It is convenient to calculate date by using RP_CALC_DATE_IN_INTERVAL ABAP function module where developers are required to add or substract a time period which can be expressed in terms of years, months or days.



SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.