You've seen many times that in standard SAP transactions the dates can be presented in days, weeks and months although in database it's saved as normal DATS type. For sure in same table you can find the field called "External date type" which is used to convert standard date to external one. If you're designing a program in which you want to use same functionallity then FM 'PERIOD_AND_DATE_CONVERT_OUTPUT' and 'PERIOD_AND_DATE_CONVERT_INPUT' comes with a help.

We would need to declare some importing and exporting parameters in a method:

  • Importing (all as optional to be able to use method in both directions):

I_EINDT TYPE EINDT OPTIONAL → DATS date
I_ELPEI TYPE TPRG-PRGBZ OPTIONAL → External date type
I_EEIND TYPE EEIND OPTIONAL → External Date
I_PRGRS TYPE TPRG-PRGRS OPTIONAL → Date type (day, week, month, interval)

  • Exporting:

E_EEIND TYPE EEIND → External date
E_EINDT TYPE EINDT → DATS date
E_PRGBZ TYPE TPRG-PRGBZ → External date type
E_PRGRS TYPE TPRG-PRGRS → Date type (day, week, month, interval)

  • Exceptions:

FAILED → Exception

Now it's time for the ABAP code of a method to convert dates from and to external format:

method date_convert.
*This is the code from http://abapblog.com.
  dataf_tprg type tprg.
  datafe_elpei type tprg-prgbz.
  if i_elpei is initial and i_prgrs is initial.
    f_tprg-prgrs '1'.
  elseif i_elpei is initial.
    f_tprg-prgrs i_prgrs.
  else.
    select single from tprg into f_tprg
    
where spras eq sy-langu
    
and prgbz eq i_elpei.
    if sy-subrc ne 0.
*  message e346(v1) with e_lpein.
      message s346(v1with i_elpei.
      raise failed.
    endif.
  endif.

*- 
  if not i_eindt is initial.
    call function 'PERIOD_AND_DATE_CONVERT_OUTPUT'
      exporting
        internal_date   i_eindt
        internal_period 
f_tprg-prgrs
      
importing
        external_date   e_eeind
        external_period 
e_prgbz
      
exceptions
        date_invalid    1
        periode_invalid 2.
    if sy-subrc ne 0.
      raise failed.
    endif.
  elseif not i_eeind is initial.
    call function 'PERIOD_AND_DATE_CONVERT_INPUT'
      exporting
        dialog_date_is_in_the_past       ' '
        external_date                    i_eeind
        external_period                  
i_elpei
*       INTERNAL_PERIOD                  = ' '
*       I_PERIV                          = I_PERIV
*       I_WERKS                          = I_WERKS
*       I_MRPPP                          = I_MRPPP
     importing
       internal_date                    e_eindt
*       INTERNAL_PERIOD                  = INTERNAL_PERIOD
     exceptions
       date_invalid                     1
       no_data                          2
       period_invalid                   3
       others                           4
              .
    if sy-subrc eq 0.
      call function 'PERIOD_AND_DATE_CONVERT_OUTPUT'
        exporting
          internal_date   e_eindt
          internal_period 
f_tprg-prgrs
        
importing
          external_date   e_eeind
          external_period 
e_prgbz
        
exceptions
          date_invalid    1
          periode_invalid 2.
      if sy-subrc ne 0.
        raise failed.
      endif.
    else.
      raise failed.
    endif.

  endif.
  e_prgrs f_tprg-prgrs.
endmethod.