In SAP ABAP, you can get the last date of a given month using the FM LAST_DAY_IN_PERIOD_GET
or by calculating it manually using date arithmetic.
1. Using Function Module LAST_DAY_IN_PERIOD_GET
DATA: lv_last_date TYPE sy-datum.
CALL FUNCTION 'LAST_DAY_IN_PERIOD_GET' EXPORTING i_gjahr = '2024' " Year i_periv = 'K4' " Fiscal year variant (use 'K4' for standard calendar year) i_poper = '03' " Month (March) IMPORTING e_date = lv_last_date.
WRITE: 'Last date of month:', lv_last_date.
- Replace
i_gjahr
with the desired year. - Replace
i_poper
with the desired month (01
for January,12
for December). i_periv = 'K4'
refers to the fiscal year variant (check your system’s fiscal variants if needed).
2. Using Date Calculation (Manual Approach)
DATA: lv_date TYPE sy-datum, lv_last_day TYPE sy-datum.
lv_date = '20240301'. " First day of the month (YYYYMMDD)
lv_last_day = lv_date + 31. " Add 31 days to ensure we move to the next monthlv_last_day = lv_last_day - ( lv_last_day+1 MOD 100 ). " Get last day of the month
WRITE: 'Last date of month:', lv_last_day.
- This approach calculates the last date by shifting the date forward and adjusting back to the last valid day of the month.
Both methods work efficiently in ABAP. Let me know if you need further clarifications! 🚀