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: 

months between dates

Former Member
0 Kudos
264

Hai

Is there any FM to calculate months between two days.

Ex: In selction date if we have given like 1.1.2013  to 1.4.2013

I want output like :1,2,3,4 months between dates.

Regards,

Sivajyothi

1 ACCEPTED SOLUTION

venkat_aileni
Contributor
0 Kudos
166

Hi-

try below code:

DATA: lv_f_cnt(2) TYPE c,

       lv_t_cnt(2) TYPE c,

       lv_cnt TYPE i.

PARAMETERS: f_date TYPE sy-datum,

             t_date TYPE sy-datum.

lv_f_cnt = f_date+4(2).

lv_t_cnt = t_date+4(2).

IF lv_f_cnt LE lv_t_cnt.

   lv_cnt = lv_t_cnt - lv_f_cnt.

   DO lv_cnt TIMES.

     IF lv_f_cnt+0(1) = '0'.

       SHIFT lv_f_cnt LEFT DELETING LEADING '0'.

     ENDIF.

     WRITE: lv_f_cnt.

     lv_f_cnt = lv_f_cnt + 1.

     IF lv_f_cnt > 12.

       lv_f_cnt = 1.

     ENDIF.

   ENDDO.

ENDIF.

Thanks,

Venkat

4 REPLIES 4

Former Member
0 Kudos
166

Try following FM...

MONTHS_BETWEEN_TWO_DATES

MONTHS_BETWEEN_TWO_DATES_NEW

HR_99S_MONTHS_BETWEEN_DATES

HR_MONTHS_BETWEEN_TWO_DATES

or open se37 and search by *month*.. you will get some FM. try those all..

Former Member
0 Kudos
166

This can be done without FM also.

data lv_start_month type i.

data lv_end_month type i.

lv_start_month = lv_start_date+4(2).        1

lv_start_year = lv_start_date+0(4).   2013

lv_end_month = lv_end_date+4(2).          4

lv_end_year = lv_end_date+0(4).  2013.

if lv_s_year eq lv_end_year.

while end_month eq start_month.

append start_month to itab.

start_month ++.

endwhile.

else.

while year become equal..

while end_month eq start_month.

append start_month to itab.

start_month ++.

endwhile.

endwhile.

then increase year.

endif.

Note: take code as algorithm. 

venkat_aileni
Contributor
0 Kudos
167

Hi-

try below code:

DATA: lv_f_cnt(2) TYPE c,

       lv_t_cnt(2) TYPE c,

       lv_cnt TYPE i.

PARAMETERS: f_date TYPE sy-datum,

             t_date TYPE sy-datum.

lv_f_cnt = f_date+4(2).

lv_t_cnt = t_date+4(2).

IF lv_f_cnt LE lv_t_cnt.

   lv_cnt = lv_t_cnt - lv_f_cnt.

   DO lv_cnt TIMES.

     IF lv_f_cnt+0(1) = '0'.

       SHIFT lv_f_cnt LEFT DELETING LEADING '0'.

     ENDIF.

     WRITE: lv_f_cnt.

     lv_f_cnt = lv_f_cnt + 1.

     IF lv_f_cnt > 12.

       lv_f_cnt = 1.

     ENDIF.

   ENDDO.

ENDIF.

Thanks,

Venkat

0 Kudos
166

Hia thanks for the rply.

It is working.