[ABAP] How do I handle time zones and date/time formatting in ABAP?

In ABAP, you can handle time zones and date/time formatting using the following methods:

  1. Time Zone Conversion: To convert a time from one time zone to another, you can use the following functions:

    • CONVERT_TIMEZONE: This function converts a time value from one time zone to another.
    • TZONET_DIFF: This function calculates the time difference between two time zones.
  2. Date/Time Formatting: To format a date or time value according to a specific format, you can use the following functions:

    • WRITE: This function formats a date or time value according to the predefined format.
    • CONVERT: This function converts a date or time value to a string using a specific format.
    • STRLEN: This function calculates the length of a string.

    Additionally, you can also use the following formatting symbols to specify the desired format:

    • YYYY: 4-digit year
    • MM: 2-digit month
    • DD: 2-digit day
    • HH: 2-digit hour (24-hour format)
    • MI: 2-digit minute
    • SS: 2-digit second

Here's an example of how you can handle time zones and date/time formatting in ABAP:

DATA: lv_time TYPE sy-uzeit, lv_time_zone TYPE ttzcu-tzone.

lv_time = sy-uzeit. lv_time_zone = 'CET'.

CALL FUNCTION 'CONVERT_TIMEZONE' EXPORTING timezone1 = sy-zonlo timezone2 = lv_time_zone time = lv_time IMPORTING converted_time = lv_time.

WRITE: / 'Current time in CET:', lv_time.

DATA(lv_date) = sy-datum.

WRITE: / 'Current date:', lv_date.

DATA(lv_formatted_date TYPE string). lv_formatted_date = lv_date+6(2) && '.' && lv_date+4(2) && '.' && lv_date(4).

WRITE: / 'Formatted date:', lv_formatted_date.

This example converts the current time to the Central European Time (CET) time zone and then displays it. It also displays the current date and formats it in the "DD.MM.YYYY" format.