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: 

subtraction of dates

Former Member
0 Kudos
152

i have 2 dates date1 date2 .. i need to find the diff in dates in days ...

help me plz .. lil urgent

7 REPLIES 7

Former Member
0 Kudos
108

Hi

Check the fun module

HR_HK_DIFF_BT_2_DATES

or write a simple code

data: date1 type sy-datum, date2 type sy-datum, days type i.

days = date1 - date2.

This will give the number of days

Reward if useful

Anji

0 Kudos
108

hey sorry i need the result in months ... can i divide the o/p by 30.

does it give right ans???

0 Kudos
108

Hi Ramesh,

Try this code, It will give month and years also. [Just copy and paste]


DATA : DATE1 LIKE SY-DATUM VALUE '19830125',
       DATE2 LIKE SY-DATUM VALUE '20070219',
       DAYS1 TYPE I,
       WEEK1 TYPE I,
       MONTH1 TYPE I,
       YEAR1 TYPE I,
       C_YEARS1 TYPE I.



CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
  EXPORTING
    i_date_from          = '20000101'
*   I_KEY_DAY_FROM       =
    i_date_to            = '20000103'
*   I_KEY_DAY_TO         =
*   I_FLG_SEPARATE       = ' '
  IMPORTING
   E_DAYS               = DAYS1
   E_MONTHS             = MONTH1
   E_YEARS              = YEAR1.
          .

 WRITE :  'DAYS   = ', DAYS1,
*          'WEEKS = ',WEEK1,
        'MONTHS = ', MONTH1,
        'YEARS   = ', YEAR1.

Thanks,

Reward If Helpful.

0 Kudos
108

Check this fm

call function 'FIMA_DAYS_AND_MONTHS_AND_YEARS'

exporting

i_date_from = FROMDATE

i_date_to = TODATE

  • I_FLG_SEPARATE = ' '

IMPORTING

E_DAYS = EDAYS

E_MONTHS = EMONTHS

E_YEARS = EYEARS.

This returns no of Days, Month and Year as a difference in two dates.

ashish

former_member387317
Active Contributor
0 Kudos
108

Hi Ramesh,

There are many ways you can do it.

=============================

PARAMETER:p_date1 TYPE dats,

p_date2 TYPE dats.

DATA:lv_diff TYPE i,

lv_no_date type i.

CALL FUNCTION '<b>DAYS_BETWEEN_TWO_DATES</b>'

EXPORTING

i_datum_bis = p_date1

i_datum_von = p_date2

IMPORTING

e_tage = lv_diff

EXCEPTIONS

days_method_not_defined = 1

OTHERS = 2.

IF sy-subrc <> 0.

WRITE:/ 'Error'.

ELSE.

WRITE:/ lv_diff.

ENDIF.

if lv_diff < 0.

lv_diff = lv_diff * -1.

endif.

lv_no_date = lv_diff - 1.

======================================

DATA: lw_date1 TYPE p0001-begda,

lw_date2 TYPE p0001-begda,

lw_date TYPE p0347-scrdd.

lw_date1 = sy-datum.

lw_date2 = '12.12.2004'.

CALL FUNCTION '<b>HR_HK_DIFF_BT_2_DATES</b>'

EXPORTING

date1 = lw_date1

date2 = lw_date2

OUTPUT_FORMAT = '02'

IMPORTING

days = lw_date.

======================================

or simply

diff = day1 - day2.

Note here day1 should be greater then day2.

=======================================

<b>Reward Points if it is helpful.</b>

Thanks & Regards

ilesh 24x7

former_member223537
Active Contributor
0 Kudos
108

HI,

HR_99S_MONTHS_BETWEEN_DATES Get months between dates

HR_ECM_GET_NUMBER_OF_MONTHS Calculates number of months between two dates

HR_GBSXP_GET_MONTHS HR-PT: Compute number of months between two dates

Best regards,

Prashant

Former Member
0 Kudos
108

Hi Ramesh,

Use the below code

<b>Method1:</b>

This is only to get the number of days between two dates.

DATA: v_tot_days TYPE i.

CALL FUNCTION 'HR_SGPBS_YRS_MTHS_DAYS'

EXPORTING

beg_da = '20070101'

end_da = '20071008'

IMPORTING

no_cal_day = v_tot_days

EXCEPTIONS

dateint_error = 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.

ENDIF.

write:/5 'Total days = ', v_tot_days.

<b>Method2:</b> This for getting the days, months, years and total days between two dates

DATA: v_days TYPE i,

v_months TYPE i,

v_years TYPE i,

v_tot_days TYPE i.

CALL FUNCTION 'HR_SGPBS_YRS_MTHS_DAYS'

EXPORTING

beg_da = '20070101'

end_da = '20071008'

IMPORTING

no_day = v_days

no_month = v_months

no_year = v_years

no_cal_day = v_tot_days

EXCEPTIONS

dateint_error = 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.

ENDIF.

write:/5 'Days = ', v_days,

/5 'Months = ', v_months,

/5 'Years = ', v_years,

/5 'Total days = ', v_tot_days.