SAP ABAP Date Functions and Operations

SAP provides built-in date functions and function modules for various date-related operations, such as date conversion, calculations, and formatting.


1. Getting the Current Date and Time

DATA: lv_date TYPE sy-datum,
lv_time TYPE sy-uzeit.
lv_date = sy-datum. " Current date (YYYYMMDD)
lv_time = sy-uzeit. " Current time (HHMMSS)
WRITE: 'Date:', lv_date, 'Time:', lv_time.

✅ Retrieves the current system date and time


2. Adding/Subtracting Days to a Date

DATA: lv_date TYPE sy-datum.
lv_date = sy-datum + 10. " Add 10 days
WRITE: 'New Date:', lv_date.
lv_date = sy-datum - 5. " Subtract 5 days
WRITE: 'Previous Date:', lv_date.

✅ Directly adds/subtracts days to DATS type variables


3. Adding/Subtracting Months and Years

Use the function module RP_CALC_DATE_IN_INTERVAL:

DATA: lv_date_new TYPE sy-datum.
CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
EXPORTING
date = sy-datum
months = 3 " Add 3 months
years = 1 " Add 1 year
IMPORTING
calc_date = lv_date_new.
WRITE: 'New Date:', lv_date_new.

✅ Handles month-end variations and leap years


4. Calculating the Difference Between Two Dates

DATA: lv_date1 TYPE sy-datum,
lv_date2 TYPE sy-datum,
lv_diff TYPE i.
lv_date1 = '20240310'. " 10th March 2024
lv_date2 = '20240301'. " 1st March 2024
lv_diff = lv_date1 - lv_date2. " Difference in days
WRITE: 'Difference in Days:', lv_diff.

✅ Calculates difference in days

For years, months, and days, use:

DATA: lv_years TYPE i,
lv_months TYPE i,
lv_days TYPE i.
CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'
EXPORTING
date1 = lv_date1
date2 = lv_date2
IMPORTING
years = lv_years
months = lv_months
days = lv_days.
WRITE: 'Difference:', lv_years, 'Years,', lv_months, 'Months,', lv_days, 'Days'.

✅ Calculates difference in years, months, and days


5. Converting Date Format

Convert date to a user-friendly format:

DATA: lv_date_ext TYPE char10.
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal = sy-datum
IMPORTING
date_external = lv_date_ext.
WRITE: 'Formatted Date:', lv_date_ext. " Outputs DD/MM/YYYY based on user settings

✅ Formats the date based on the user’s SAP locale

Convert a formatted date (DD/MM/YYYY) to SAP format (YYYYMMDD):

DATA: lv_date_int TYPE sy-datum.
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = '07/03/2025'
IMPORTING
date_internal = lv_date_int.
WRITE: 'SAP Date:', lv_date_int. " Outputs 20250307

✅ Converts date strings from external systems


6. Getting the First and Last Day of a Month

Use LAST_DAY_IN_PERIOD_GET:

DATA: lv_last_day TYPE sy-datum.
CALL FUNCTION 'LAST_DAY_IN_PERIOD_GET'
EXPORTING
i_gjahr = '2024' " Year
i_periv = 'K4' " Fiscal year variant (Standard: K4)
i_poper = '03' " Period (March)
IMPORTING
e_date = lv_last_day.
WRITE: 'Last Day of Month:', lv_last_day.

✅ Returns the last day of a month in a fiscal year

For the first day, subtract days:

DATA: lv_first_day TYPE sy-datum.
lv_first_day = sy-datum - ( sy-datum+6(2) - 1 ).
WRITE: 'First Day:', lv_first_day.

✅ Extracts first day by adjusting the day component


7. Working with Timestamps

Get the current UTC timestamp:

DATA: lv_timestamp TYPE timestamp.
GET TIME STAMP FIELD lv_timestamp.
WRITE: 'Current UTC Timestamp:', lv_timestamp.

✅ Precise to seconds

Calculate timestamp difference in seconds:

DATA: lv_tstmp1 TYPE timestamp,
lv_tstmp2 TYPE timestamp,
lv_diff TYPE int8.
lv_tstmp1 = '20240310120000'. " 10th March 2024, 12:00:00
lv_tstmp2 = '20240309180000'. " 9th March 2024, 18:00:00
lv_diff = cl_abap_tstmp=>subtract(
i_tstmp1 = lv_tstmp1
i_tstmp2 = lv_tstmp2 ).
WRITE: 'Difference in Seconds:', lv_diff.

✅ Works for precise time calculations


8. Factory Calendar & Business Days

To calculate next business day, use:

DATA: lv_next_date TYPE sy-datum.
CALL FUNCTION 'DATE_NEXT_WORKDAY'
EXPORTING
start_date = sy-datum
factory_calendar = 'US' " Set factory calendar
IMPORTING
next_workday = lv_next_date.
WRITE: 'Next Business Day:', lv_next_date.

✅ Uses a factory calendar for holidays

To calculate working days between two dates, use:

DATA: lv_days TYPE i.
CALL FUNCTION 'DAYS_BETWEEN_TWO_DATES'
EXPORTING
date_1 = '20240301'
date_2 = '20240310'
IMPORTING
days = lv_days.
WRITE: 'Working Days:', lv_days.

✅ Excludes weekends/holidays based on SAP settings


Summary: Key Date Functions

OperationMethod
Get current date/timesy-datum, sy-uzeit
Add/subtract daysdate + n or date - n
Add/subtract months/yearsRP_CALC_DATE_IN_INTERVAL
Date difference (days)date1 - date2
Date difference (years, months, days)HR_HK_DIFF_BT_2_DATES
Convert to user formatCONVERT_DATE_TO_EXTERNAL
Convert user input to SAP dateCONVERT_DATE_TO_INTERNAL
First/Last day of a monthLAST_DAY_IN_PERIOD_GET
Get UTC timestampGET TIME STAMP
Timestamp difference (seconds)CL_ABAP_TSTMP=>SUBTRACT
Next business dayDATE_NEXT_WORKDAY
Working days between datesDAYS_BETWEEN_TWO_DATES