SAP Tutorials Blog


 


DATA date_calc_1 TYPE d VALUE '20240101'.
DATA date_calc_2 TYPE d VALUE '20231227'.
DATA date_calc_3 TYPE d VALUE '20231230'.
DATA date_calc_4 TYPE d VALUE '20240220'.

DATA(days_diff_1) = date_calc_1 - date_calc_2. "5
DATA(days_diff_2) = date_calc_1 - date_calc_3. "2
DATA(days_since_01_01_0001) = CONV i( date_calc_1 ). "738887
"Getting the weekday (1 = Monday, 2 = Tuesday, ...)
DATA(weekday1) = ( 5 + date_calc_1  MOD 7 ) MOD 7 + 1. "1 (Monday)
DATA(weekday2) = ( 5 + date_calc_3  MOD 7 ) MOD 7 + 1. "6 (Saturday)
DATA(date_w_first_day_of_month) =  CONV d( replace( val = `202403020` off = 6 len = 2 with = `01` ) ). "20240301
DATA(date_w_last_day_of_prev_month) = CONV d( date_w_first_day_of_month - 1 ). "20240229

*&---------------------------------------------------------------------*
*& Performing date additions and subtractions using the XCO library
*&---------------------------------------------------------------------*

"Adding days to the current date using the 'add' method
"e.g. 2024-03-16 (if the current date is 2024-03-11)
DATA(xco_date_add_5days) = xco_cp=>sy->date( )->add( iv_day = 5
                                             )->as( xco_cp_time=>format->iso_8601_extended
                                             )->value.

"The 'add' method has various parameters, adding 1 day/month/year
"e.g. 2025-04-12 (if the current date is 2024-03-11)
DATA(xco_date_add_1_mult) = xco_cp=>sy->date( )->add( iv_day = 1 iv_month = 1 iv_year = 1
                                              )->as( xco_cp_time=>format->iso_8601_extended
                                              )->value.

"Addition with a created date
"2024-02-29
DATA(xco_date_add_1day_custom) = xco_cp_time=>date( iv_year = 2024 iv_month = 02 iv_day = 28
                                                  )->add( iv_day = 1
                                                  )->as( xco_cp_time=>format->iso_8601_extended
                                                  )->value.

"Subtracting using the 'subtract' method
"e.g. 2023-02-10 (if the current date is 2024-03-11)
DATA(xco_date_subtr_1_mult) = xco_cp=>sy->date( )->subtract( iv_day = 1 iv_month = 1 iv_year = 1
                                                )->as( xco_cp_time=>format->iso_8601_extended
                                                )->value.

"Optional parameter io_calculation
"io_calculation parameter: xco_cp_time=>date_calculation->preserving,
"i.e. the date is calculated mathmatically and preserved. It is the
"default.
"In case of an invalid resulting date, an exception is raised.
TRY.
    DATA(inv_date_a) = xco_cp_time=>date( iv_year  = '2024'
                                          iv_month = '08'
                                          iv_day   = '31'
                                        )->add( iv_month = 1
                                                io_calculation = xco_cp_time=>date_calculation->preserving
                                        )->as( xco_cp_time=>format->iso_8601_extended
                                        )->value.
  CATCH cx_root.
ENDTRY.

"io_calculation parameter: xco_cp_time=>date_calculation->ultimo,
"Here, the actual last day of the month is considered. If the calculated
"date is invalid, the ultimo is automatically used. In the example, one month is added to
"the date. However, September does not have 31 days, so the result is adjusted to the actual
"last day of September, which is 30 in this case.
"Result: 2024-09-30
TRY.
    DATA(inv_date_b) = xco_cp_time=>date( iv_year  = '2024'
                                          iv_month = '08'
                                          iv_day   = '31'
                                        )->add( iv_month = 1
                                                io_calculation = xco_cp_time=>date_calculation->ultimo
                                        )->as( xco_cp_time=>format->iso_8601_extended
                                        )->value.
  CATCH cx_root.
ENDTRY.

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