Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Moderation comment: an updated version of this blog post can found here.

 

Sometimes formatting a date in ABAP can be very useful:

 

- The 1st example shows how to convert an internal date (i.e. sy-datum) to an external variable  i.e. a char:

  • INPUT:     21.06.2018 ( as a date datatype)

  • OUTPUT: '21.06.2018' (as character)


DATA date_ext TYPE char10.

CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal = sy-datum
IMPORTING
date_external = date_ext
EXCEPTIONS
date_internal_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.


 

- The 2nd example shows how to convert an external date format (like a character) to an internal date format:

  • INPUT: '21.06.2018' (as character)

  • OUTPUT: 21062018 (as a date datatype)


DATA date_int TYPE d.

CALL FUNCTION 'DATE_CONV_EXT_TO_INT'
EXPORTING
i_date_ext = '21.06.2018'
IMPORTING
e_date_int = date_int
EXCEPTIONS
error = 1
OTHERS = 2.

IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

 

- The 3rd example shows how you can convert a SAP date datatype to a character on your own without the help of an function module:

  • INPUT: 21.06.2018 (as sy-datum)

  • OUTPUT: '21062018' (as character)


DATA: date(10),
new_date TYPE char10,
day(2),
month(2),
year(4).

WRITE sy-datum TO date.

day(2) = date(2).
month(2) = date+3(2).
year(4) = date+6(4).
new_date = |{ day }| && |{ month }| && |{ year }|.

 
6 Comments