Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

List Months between a date range

Former Member

Hi, I'm trying to list the months between a given date range in an ABAP report to generate a MIS report(ALV).. Say I have 2 date ranges, 01.01.2006 to 01.04.2006.. I want to list

Jan 2006

Feb 2006

Mar 2006

Apr 2006.. Any FM in SAP to get this OP..

5 REPLIES 5

Former Member
0 Kudos

u can try creating an select options and appending with the values in low & high and selectop-options = 'BT'

0 Kudos

Not as per Question.

Former Member
0 Kudos

hi vivek,

use this function module to get months beween two dates

1. MONTHS_BETWEEN_TWO_DATES or

2. MONTHS_BETWEEN_TWO_DATES_NEW

please reward the point if you are satisfied with answer

thanks,

john.

viva_kd
Explorer
0 Kudos

DID u get solution , any FM .please post.

jmodaal
Active Contributor
0 Kudos

Hello viva.kd,

you can use function module MONTH_NAMES_GET for this purpose.

program Z_TEST2.
parameters: pdate1 type d default '20060101',
pdate2 type d default '20060401'.
types: begin of TMonth,
monthAbbr type char3,
year type numc4,
end of TMonth.
data: monthsBtDates type standard table of TMonth with empty key,
monthBtDates type TMonth.
data: rc type syst_subrc,
months type standard table of T247.
" Get all months with their names
call function 'MONTH_NAMES_GET'
IMPORTING
RETURN_CODE = rc
TABLES
MONTH_NAMES = months
EXCEPTIONS
MONTH_NAMES_NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
exit.
ENDIF.
perform getMonthsBtDates using pdate1 pdate2.
loop at monthsBtDates assigning field-symbol(<mBtDates>).
write:/ <mBtDates>-monthAbbr, <mBtDates>-year.
endloop.
*
form getMonthsBtDates using d1 type d d2 type d.
data: currentDate type d,
currentMonth type numc2.
currentDate = d1.
clear monthsBtDates.
while currentDate <= d2.
currentMonth = currentDate+4(2).
read table months with key mnr = currentMonth
assigning field-symbol(<month>).
if sy-subrc = 0.
monthBtDates-monthAbbr = <month>-ktx.
monthBtDates-year = currentDate(4).
append monthBtDates to monthsBtDates.
endif.
" Now move to next month
currentDate+6(2) = '28'.
add 4 to currentDate.
currentDate+6(2) = '01'.
endwhile.
endform.

Kind regards and a happy new year

Jan