I'd like to format a time (SY-TIMLO) to the user defined format in SU01. For US users, this often involves AM/PM. The easiest way I've found is:
lv_formatted_time = |{ lv_time TIME = USER }|
Is there an easy way to remove the seconds without the use of more extensive methods like the one below?
*-- Convert time to user time format based on environment setting.
*-- As we are not using SET COUNTRY this has the same effect as
*-- user settings in SU01 > Defaults > Time Format. US users will see AM/PM.
    CALL METHOD CL_ABAP_TIMEFM=>CONV_TIME_INT_TO_EXT
      EXPORTING
        TIME_INT            = lv_time
        WITHOUT_SECONDS     = ABAP_TRUE
        FORMAT_ACCORDING_TO = CL_ABAP_TIMEFM=>ENVIRONMENT
      IMPORTING
        TIME_EXT            = lv_formatted_time.
                As far as I can see, this is not trivially possible using the string templates. I would recommend to stick to the method you already mentioned. If you're interested in shortening the code and since you'll probably always use the same values for WITHOUT_SECONDS and FORMAT_ACCORDING_TO, you could wrap it in a method with a returning parameter so that your code would then contain lines like
lv_formatted_time = format_time_wo_seconds( lv_time ).
As an alternative, you might use a macro - I'm not a huge fan of these for modularization, but this is one of the places where they do come handy:
DEFINE format_time_short.
  CALL METHOD CL_ABAP_TIMEFM=>CONV_TIME_INT_TO_EXT
    EXPORTING
      TIME_INT            = &1
      WITHOUT_SECONDS     = ABAP_TRUE
      FORMAT_ACCORDING_TO = CL_ABAP_TIMEFM=>ENVIRONMENT
    IMPORTING
      TIME_EXT            = &2.
END-OF-DEFINITION.
format_time_short lv_time lv_formatted_time.
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With