SAP Tutorials Blog


 


"Retrieving the current date with respect to UTC, e.g. 20240101
"The result's base type is the DDIC type dats that is mapped to the
"ABAP type d.
DATA(utc_date) = cl_abap_context_info=>get_system_date( ).

"Using XCO
"Notes:
"- The results of the following chained statements are of type string.
"- With the 'as' method, a given format (available via xco_cp_time=>format)
"  is applied to the time. Several options are available.
"- The 'date' method has an optional importing parameter for specifying the
"  time zone. By default, the user's time zone is used. For the specification,
"  you can use xco_cp_time=>time_zone.

"The following three examples:
"- do not specify the optional importing parameter for the 'date' method. In
"  this case, the user time zone is used implicitly.
"- specify a specific format in the 'as' method.

"e.g. 20250101
DATA(date_abap) = xco_cp=>sy->date( )->as( xco_cp_time=>format->abap )->value.

"e.g. 20250101
DATA(date_basic) = xco_cp=>sy->date( )->as( xco_cp_time=>format->iso_8601_basic )->value.

"e.g. 2025-01-01
DATA(date_ext) = xco_cp=>sy->date( )->as( xco_cp_time=>format->iso_8601_extended )->value.

"Specifying the time zone explicitly in the 'date' method
"The following method call retrieves the current user date.
DATA(date_user_tz) = xco_cp=>sy->date( xco_cp_time=>time_zone->user
                                )->as( xco_cp_time=>format->iso_8601_basic
                                )->value.

"Specifying the UTC time zone
DATA(date_utc_tz) = xco_cp=>sy->date( xco_cp_time=>time_zone->utc
                                )->as( xco_cp_time=>format->iso_8601_basic
                                )->value.

*&---------------------------------------------------------------------*
*& Retrieving the current date values using XCO
*&---------------------------------------------------------------------*

"e.g. 01
DATA(day) = xco_cp=>sy->date( )->day.
"e.g. 01
DATA(month) = xco_cp=>sy->date( )->month.
"e.g. 2024
DATA(year) = xco_cp=>sy->date( )->year.

*&---------------------------------------------------------------------*
*& Creating dates
*&---------------------------------------------------------------------*

DATA date_cr1 TYPE d.
date_cr1 = '20240101'.
DATA date_cr2 TYPE d VALUE '20240202'.
DATA(date_cr3) = CONV d( '20240303' ).

"Using XCO
"The result contains a reference. It can be used for further processing
"with the XCO library.
DATA(xco_date) = xco_cp_time=>date( iv_year = 2024
                                    iv_month = 3
                                    iv_day = 3 ).

"Examples
"2024-03-03
DATA(date_cr4) = xco_date->as( xco_cp_time=>format->iso_8601_extended )->value.
"03
DATA(day_from_date) = xco_date->day.

Serkan AKKAVAK Computer Engineer BSc Head of SAP & Software Department Contact : serkurumsal@yandex.com