Application Development and Automation 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: 
Read only

Calculating EXACT months between 2 dates

Former Member
0 Likes
4,864

Hi,

Does anyone know a function to do the equivalent to Oracle function "MONTHS_BETWEEN". i.e. to work out the EXACT difference in decimals between 2 dates e.g 2.123432 could be a valid result. Thanks,

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
4,537

hi try these

FIMA_DAYS_AND_MONTHS_AND_YEARS

HR_HK_DIFF_BT_2_DATES

regards

kartik

Hi,

Does anyone know a function to do the equivalent to Oracle function "MONTHS_BETWEEN". i.e. to work out the EXACT difference in decimals between 2 dates e.g 2.123432 could be a valid result. Thanks,

9 REPLIES 9
Read only

Former Member
0 Likes
4,534

Hello,

Use FM 'HR_99S_INTERVAL_BETWEEN_DATES'.

Thanks,

Jayant

Read only

Former Member
0 Likes
4,538

hi try these

FIMA_DAYS_AND_MONTHS_AND_YEARS

HR_HK_DIFF_BT_2_DATES

regards

kartik

Read only

0 Likes
4,534

I tested

this Fm FIMA_DAYS_AND_MONTHS_AND_YEARS

it seems to satisfy ur requirements as mentioned by u above

Read only

0 Likes
4,534

Hi,

Please provide some sample test data as i am not getting any decimals? Thanks,

Read only

0 Likes
4,534

sorry for my above post. that was a mistake

try HR_HK_DIFF_BT_2_DATES

it takes 3 import parameters date1 , date2 and output format.

give output format as 04 i.e months with decimal

test data:

date1 = 2009/01/01

date2 = 2008/11/10

output_format = 04

result:

YEARS 0.0000

MONTHS 1.7562

DAYS 0

note:

YF Output format text

01 Years with decimals

04 Months with decimals

07 Anniversary

08 Anniversary months

E1 Years with decimals

E4 Months with decimals

E7 Anniversary

E8 Anniversary months

Edited by: kartik tarla on Nov 13, 2008 7:34 PM

Read only

0 Likes
4,534

if you are not getting the decimals check the type of variable you are capturing the number of weeks

it need to be of type p decimals 4.

datA: lv_weeks type p decimals 4.

Read only

0 Likes
4,534

Hi Samir,

Here is the code for you.


REPORT  zcc_test .
data: v_days type VTBBEWE-ATAGE.
data: v_months type VTBBEWE-ATAGE.
data: v_year type VTBBEWE-ATAGE.

CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
  EXPORTING
    i_date_from          = '20081101'
*   I_KEY_DAY_FROM       =
    i_date_to            = '20081112'
*   I_KEY_DAY_TO         =
*   I_FLG_SEPARATE       = ' '
 IMPORTING
   E_DAYS               = v_days
   E_MONTHS             = v_months
   E_YEARS              = v_year
          .

write: / 'Days', v_days, 'Month', v_months, 'Year', v_year.
          .

Thanks,

Chidanand

Read only

0 Likes
4,534

No Problem. However FM HR_HK_DIFF_BT_2_DATES doesnt seem to be available in our system when i try to search for it ! Any ideas why not - we are on SAP NetWeaver 2004s?

Read only

0 Likes
4,534

this is the code in that FM use it directly.

*"----


""Local interface:

*" IMPORTING

*" VALUE(DATE1) LIKE P0001-BEGDA

*" VALUE(DATE2) LIKE P0001-BEGDA

*" VALUE(OUTPUT_FORMAT) LIKE T7HK2B-SCALC DEFAULT '01'

*" EXPORTING

*" VALUE(YEARS) LIKE P0347-SCRYY

*" VALUE(MONTHS) LIKE P0347-SCRMM

*" VALUE(DAYS) LIKE P0347-SCRDD

*" EXCEPTIONS

*" INVALID_DATES_SPECIFIED

*"----


data: l_diff_years(2) type n, "Number of years counter

l_diff_months(2) type n, "Number of months counter

l_diff_days(2) type n, "Number of days counter

p_diff_years(3) type p, "Number of years counter

p_diff_months(3) type p, "Number of months counter

p_diff_days(3) type p, "Number of days counter

date_anni_months(1) type c,

date_anni_years(1) type c,

date_begda type d,

date_endda type d,

date_out type d,

l_date_last1 type d,

l_date_last2 type d,

corresponding_date_in_year type d,

corresponding_date_in_month type d.

data: l_date3 type d, "Diff bt 2 dates :Note 0361169

l_calc_date type d, "Calculation date :Note 0361169

l_total_number_days type p. "Total num of days :Note 0361169

data l_temp_date type d. "Temp date :Note 0368547

  • Check if DATE2 is greater than DATE1 and if so, raise an exception.

if date2 > date1.

raise invalid_dates_specified.

endif.

date_begda = date2.

date_endda = date1.

date_out = date2. "will be changed by routines

  • Modification for Note 0361169

  • Add Block Begin

l_calc_date = date2.

l_date3 = ( date1 + 1 ) - date2.

  • If the number of days isn't zero, subtract 1.

if l_date3+6(2) <> 0.

l_date3+6(2) = l_date3+6(2) - 1.

endif.

  • If the number of months isn't zero, subtract 1.

if l_date3+4(2) <> 0.

l_date3+4(2) = l_date3+4(2) - 1.

endif.

  • If the number of years isn't zero, subtract 1.

if l_date3+0(4) <> 0.

l_date3+0(4) = l_date3+0(4) - 1.

endif.

  • Note 368547 delete begin:

  • Add the number of years different to the lower date (date2).

  • l_calc_date0(4) = date20(4) + l_date3+0(4).

  • Subtract this modified date from the upper date (date1) and place

  • the result into a packed field so the resultis displayed in only

  • days.

  • l_total_number_days = ( date1 + 1 ) - l_calc_date.

  • Note 368547 delete end.

  • Add Block End : Note 0361169

  • Format the years, months and days based on the output_format.

case output_format.

when '01'.

  • Format: Years with decimals.

  • Modification for Note 0361169

  • Delete Block Begin

  • perform find_corresp_date_in_year

  • using date_begda

  • date_endda

  • changing p_diff_years

  • corresponding_date_in_year.

*

  • days = date_endda - corresponding_date_in_year + 1.

  • months = 0.

  • years = p_diff_years + days / days_in_year.

  • clear days .

  • Delete Block End : Note 0361169

  • Note 368547 add begin:

l_temp_date = date1.

l_temp_date+4(4) = '0301'.

l_temp_date = l_temp_date - 1.

if l_calc_date+4(4) = '0229'.

l_calc_date = l_calc_date + 1.

  • Note 679872 liuyo 20031112

  • if l_temp_date+4(4) = '0229'.

  • l_calc_date = l_calc_date - 1.

  • endif.

endif.

l_calc_date+0(4) = date2+0(4) + l_date3+0(4).

l_total_number_days = ( date1 + 1 ) - l_calc_date.

  • Note 679872 liuyo 20031112

if l_temp_date+4(4) = '0229' and l_calc_date+4(4) = '0301'.

l_total_number_days = l_total_number_days + 1.

endif.

  • Note 368547 add end.

  • Modification for Note 0361169

  • Add Block Begin

years = ( l_date3+0(4) + ( l_total_number_days / days_in_year ) ).

months = 0.

days = 0.

  • Add Block End : Note 0361169

when '02'.

  • Format : Days.

years = 0.

months = 0.

days = date_endda - date_begda + 1.

when '03'.

  • Format: Years and days.

perform find_corresp_date_in_year

using date_begda

date_endda

changing p_diff_years

corresponding_date_in_year.

years = p_diff_years.

months = 0.

days = date_endda - corresponding_date_in_year + 1.

when '04'.

  • Format: months with decimals.

perform find_corresp_date_in_year

using date_begda

date_endda

changing p_diff_years

corresponding_date_in_year.

months = 0.

perform find_corresp_date_in_month

using corresponding_date_in_year

date_endda

changing months

corresponding_date_in_month.

days = date_endda - corresponding_date_in_month + 1.

years = 0.

months = months

+ p_diff_years * months_in_year

+ days / days_in_month.

days = 0.

when '05'.

  • Format: Years, months and days.

perform find_corresp_date_in_year

using date_begda

date_endda

changing p_diff_years

corresponding_date_in_year.

years = p_diff_years.

months = 0.

perform find_corresp_date_in_month

using corresponding_date_in_year

date_endda

changing months

corresponding_date_in_month.

days = date_endda - corresponding_date_in_month + 1.

when '06'.

  • Format: Years and months.

perform find_corresp_date_in_year

using date_begda

date_endda

changing p_diff_years

corresponding_date_in_year.

years = p_diff_years.

months = 0.

perform find_corresp_date_in_month

using corresponding_date_in_year

date_endda

changing months

corresponding_date_in_month.

perform last_day_in_month(sapfp500) using date_endda

l_date_last1.

if date_endda = l_date_last1

and corresponding_date_in_month+6(2) = '01'.

months = months + 1.

else.

days = date_endda - corresponding_date_in_month + 1.

months = months + days / days_in_month.

endif.

days = 0.

when '07'.

  • Format : Anniversary years

p_diff_years = date_endda+0(4) - date_begda+0(4).

perform check_anni_years using date_begda

date_endda

changing date_anni_years.

if date_anni_years = 'Y'.

p_diff_years = p_diff_years + 1.

elseif p_diff_years > 0.

perform day_plus_years(sapfp500) using date_begda

p_diff_years

date_out.

date_out = date_out - 1. "give end date of anni. mon

if date_endda <= date_out.

p_diff_years = p_diff_years - 1.

endif.

endif.

years = p_diff_years.

months = 0.

days = 0.

when '08'.

  • Format : Anniversary months

p_diff_years = date_endda+0(4) - date_begda+0(4).

p_diff_months = date_endda+4(2) - date_begda+4(2).

if p_diff_years > 0.

p_diff_months = p_diff_years * months_in_year

+ p_diff_months.

endif.

perform check_anni_months using date_begda

date_endda

changing date_anni_months.

if date_anni_months = 'Y'.

p_diff_months = p_diff_months + 1.

elseif p_diff_months > 0.

perform day_plus_months(sapfp500) using date_begda

p_diff_months

date_out.

perform last_day_in_month(sapfp500) using date_begda

l_date_last1.

perform last_day_in_month(sapfp500) using date_endda

l_date_last2.

date_out = date_out - 1. "give end date of anni. mon

if l_date_last16(2) = l_date_last26(2)." 30 = 30 OR 31 =31

if date_endda <= date_out.

p_diff_months = p_diff_months - 1.

endif.

elseif l_date_last16(2) > l_date_last26(2).

if date_begda6(2) = l_date_last16(2)

and date_endda6(2) = l_date_last26(2).

else.

if date_endda > date_out

and date_begda6(2) ne l_date_last16(2)

and date_endda6(2) eq l_date_last26(2) .

  • p_diff_months = p_diff_months - 1.

    • note 496952

elseif date_endda <= date_out.

p_diff_months = p_diff_months - 1.

endif.

endif.

else. " l_date_last1 < l_date_last2.

if date_begda6(2) = l_date_last16(2)

and date_endda6(2) = l_date_last26(2).

else.

if date_endda > date_out

and date_begda6(2) = l_date_last16(2)

and date_endda6(2) ne l_date_last26(2) .

  • p_diff_months = p_diff_months - 1.

    • note 496952 ( for the case, from 27.02 to 27.03 is 1 month,

*but 28.02 to 28.03 is not one month

elseif date_endda <= date_out.

p_diff_months = p_diff_months - 1.

endif.

endif.

endif.

endif.

years = 0.

months = p_diff_months. "assign the # of anni month.

days = 0.

when others.

  • Format: Others.

years = 0.

months = 0.

days = 0.

endcase.