Formatting SAP date field using ABAP into any format such as DDMMYYY, MM/DD/YYYY, DD-MMM-YY...
Below is some ABAP code to demonstrate a number of ways to format a SAP date value:
Using the WRITE statement
The first method is using the WRITE statement
i.e. WRITE ld_sourcefield TO ld_resultfield DD/MM/YY.
...other options available for date format using WRITE TO
[ DD/MM/YY | MM/DD/YY
| DD/MM/YYYY | MM/DD/YYYY
| DDMMYY | MMDDYY
| YYMMDD ] ... .
*using the WRITE TO statement
*****************************
data: gd_date(10). "field to store output date
* Converts SAP date from 20020901 to 01.09.2002
write sy-datum to gd_date dd/mm/yyyy.
* Converts SAP date from 20020901 to 01.09.02
write sy-datum to gd_date dd/mm/yy.
* Converts SAP date from 20020901 to 09.01.02
write sy-datum to gd_date mm/dd/yy.
* Converts SAP date from 20020901 to 020901
write sy-datum to gd_date yymmdd.
"....
Using offset and string manipulation techniques
* Using basic data manipulation techniques
************************************
data: gd_date(8). "field to store output date
* Converts SAP date from 20010901(YYYYMMDD) to 01092001(DDMMYYYY)
gd_date(2) = sy-datum+6(2).
gd_date+2(2) = sy-datum+4(2).
gd_date+4(4) = sy-datum(4).
*....
* taking this technique bit further you can produce any output you want
************************************
data: gd_outputdate type string,
gd_day(2), "field to store day 'DD'
gd_month(2), "field to store month 'MM'
gd_year(4). "field to store year 'YYYY'
* split date into 3 fields to store 'DD', 'MM' and 'YYYY'
gd_day(2) = sy-datum+6(2).
gd_month(2) = sy-datum+4(2).
gd_year(4) = sy-datum(4).
*Now use the CONCATENATE statement or it's newer method to format the date as you want
*Note you could also at this point create new variables to represent MMM i.e. APR, JAN, FEB etc
*dd/mm/yyyy
concatenate gd_day gd_month gd_year into gd_outputdate separated by '/'.
*dd.mm.yyyy
concatenate gd_day gd_month gd_year into gd_outputdate separated by '.'.
*mm--dd--yyyy
concatenate gd_month gd_day gd_year into gd_outputdate separated by '--'.
*mm-dd-yyyy using new method
gd_outputdate = |{ gd_month }| && |-| && |{ gd_day }| && |-| && |{ gd_year }|.
*mmyyyy using new method
gd_outputdate = |{ gd_month }| && |{ gd_year }|.
Using function module to convert to DDMMMYYYY
This method is quite simple you simply pass the date field into function module CONVERSION_EXIT_IDATE_OUTPUT and it returns the date in the format DDMMMYYYY. Also if you do a search in SE37 using *DATE* or *DATE*OUTPUT* you will find many more function modules that do a similar job.
* Using Function modules
************************
data: gd_date(8). "field to store output date
* Converts date from 20010901 to 01SEP2001
gd_date = sy-datum.
CALL FUNCTION 'CONVERSION_EXIT_IDATE_OUTPUT'
EXPORTING
input = gd_date
IMPORTING
OUTPUT = gd_date.