# Timestamp display in ALV

- Link: https://ultimasolution.pl/timestamp-display-in-alv/
- Published: 2020-11-04T22:30:27+01:00
- Author: Karol Preiskorn

In ABAP, when displaying a timestamp in an ALV (ABAP List Viewer), you typically
use the `CL_SALV_TABLE` or `REUSE_ALV_GRID_DISPLAY` functions to present data, including
timestamp fields. The timestamp field is often stored in the `TIMESTAMP` or `TIMESTAMPL`
format (`CHAR` type with length 14 or 21 respectively). To display it properly in
an ALV, you can use formatting options or convert it into a readable format.

Here’s how you can display a timestamp in ALV

### Example using `CL_SALV_TABLE`:

    ```EnlighterJSRAW
    DATA: lt_data TYPE TABLE OF ztable, " Replace ztable with your table
          lr_alv  TYPE REF TO cl_salv_table,
          lt_fieldcatalog TYPE salv_t_fieldcat_alv.

    " Select data from your table with timestamp fields
    SELECT * FROM ztable INTO TABLE lt_data.

    " Create the ALV instance
    cl_salv_table=>factory( 
      IMPORTING r_salv_table = lr_alv 
      CHANGING t_table = lt_data ).

    " Optionally, format the timestamp field to be more readable
    DATA: ls_columns TYPE REF TO cl_salv_columns_table,
          ls_column  TYPE REF TO cl_salv_column.

    ls_columns = lr_alv->get_columns( ).

    " Get the timestamp field for formatting
    ls_column = ls_columns->get_column( 'TIMESTAMP_FIELD' ). " Replace with actual field name

    " Set output length (if required) and other properties
    ls_column->set_output_length( 21 ).

    " Display the ALV
    lr_alv->display( ).
    ```

Example using `REUSE_ALV_GRID_DISPLAY`:

    ```EnlighterJSRAW
    DATA: lt_data          TYPE TABLE OF ztable, " Replace ztable with your table
          lt_fieldcatalog  TYPE slis_t_fieldcat_alv,
          ls_fieldcatalog  TYPE slis_fieldcat_alv.

    " Select data with timestamp fields
    SELECT * FROM ztable INTO TABLE
    ```

I have a time stamp which is displayed as `'20,201,004,223,429'`. I want to show
this in ALV formatted as `'2020-10-04 22:34'`.

    ```EnlighterJSRAW
    FORM modify_fieldcat.
      LOOP AT i_fieldcat ASSIGNING .
        CASE -fieldname.
        WHEN 'TIMESTAMP'.
            -edit = ' '.
            -col_opt = 'X'.
            -edit_mask = '____-__-__ __:__'.
        ENDCASE.
    ENDFORM.
    ```

### Converting Date & Time to Timestamp or vice versa

Assume date and time is only available, but the target value required was timestamps.
In this case, the date and time needs to be converted/combined as a timestamp.

In other way, assume if only timestamp is available but the date and time required
separately. This case, the timestamp should be split into date and time.

### Syntax: Timestamp from date & time

    ```EnlighterJSRAW
    CONVERT DATE w_date [TIME w_time [DAYLIGHT SAVING TIME w_dst]] INTO TIME STAMP w_tstamp TIME ZONE w_tzone.
    ```

### Syntax – Split timestamp to date & time

    ```EnlighterJSRAW
    CONVERT TIME STAMP w_tstamp TIME ZONE w_tzone INTO [ DATE w_date] [ TIME w_time ] [DAYLIGHT SAVING TIME w_dst].
    ```
