简体   繁体   English

在 ABAP 中格式化时间,删除秒

[英]Format time in ABAP, removing seconds

I'd like to format a time (SY-TIMLO) to the user defined format in SU01.我想在 SU01 中将时间(SY-TIMLO)格式化为用户定义的格式。 For US users, this often involves AM/PM.对于美国用户,这通常涉及 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如果您有兴趣缩短代码并且因为您可能总是对WITHOUT_SECONDSFORMAT_ACCORDING_TO使用相同的值,您可以将它包装在一个带有返回参数的方法中,以便您的代码将包含类似的行

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM