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

difference between two dates

Former Member
0 Likes
1,052

Hello Experts,

I have a doubt. I have two dates D1 and D2. I need to see that if D1 is a date that falls within next 2 years of D2 then it should throw an error. Can you please tell me how to go about that.

thanks and regards,

Gaurav

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,015
DATA: v_date LIKE RM06B-EEIND. Say this as D2
DATA: date LIKE sy-datum.
v_date = '01/01/2007'. D2
CALL FUNCTION 'DATE_IN_FUTURE'
  EXPORTING
    anzahl_tage                   = '24'
    import_datum                  = v_date
 IMPORTING
*   EXPORT_DATUM_EXT_FORMAT       =
   EXPORT_DATUM_INT_FORMAT       = date
          .
If ( D1 <=  date 
and D1 >= D2 ). ( Check if it is between D2 and date- 2years+D2 )
Message e000 with 'Error;.
ENDIF.

I will explain u teh logic if any mistakes are there check and change it.

The FM is used to get the next two years from the given date, that is y i have paseed 24 if u want previous 24 u can pass -24.

Then I am checking whether the date D1 is between the date D2 and the next two years, if so error message.

Reward if this helsp.

9 REPLIES 9
Read only

Former Member
0 Likes
1,015

Hi!

1. variant

lv_diff = d2 - d1.

If lv_diff > 730.

*error

ENDIF.

2.nd variant

lv_diff = d2(4) - d1(4).

IF lv_diff > 2.

  • error

ENDIF.

Regards

Tamá

Read only

Former Member
0 Likes
1,015

you can use this FM

FIMA_DAYS_AND_MONTHS_AND_YEARS

Read only

Former Member
0 Likes
1,015

Helo Gaurav,

Do like this



Data: lv_datum like sy-datum.

d3 = d2 + 730 .  " for two years.

if D1 between d2 and d3.
error message.                 " error message
endif.

Regards,

Vasanth

Read only

Former Member
0 Likes
1,015
chk this from other thread

REPORT ZDATEDIFF.
 
DATA: EDAYS   LIKE VTBBEWE-ATAGE,
      EMONTHS LIKE VTBBEWE-ATAGE,
      EYEARS  LIKE VTBBEWE-ATAGE.
 
PARAMETERS: FROMDATE LIKE VTBBEWE-DBERVON,
            TODATE   LIKE VTBBEWE-DBERBIS DEFAULT SY-DATUM.
 
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.
 
if EYEARS  < 2 years throw an error
Read only

Former Member
0 Likes
1,015

hi Gaurav,

Please try the following logic:


data:
    l_date type sy-datum,
    l_date2 type sy-datum,
    l_diff type i,
    l_diff_years type i.

    l_date = '20050101'.
    l_date2 = '200070110'.
  CALL FUNCTION 'C14B_DIFF_BT_2_DATES'
  EXPORTING
      i_date_from                     = l_date    
      i_date_to                       = l_date2      
  IMPORTING
      E_DAYS                          = l_diff
      E_YEARS                       = l_diff_years
  EXCEPTIONS
      PLAUSIBILITY_CHECK_FAILED       = 1
      OTHERS                          = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
if l_diff > 730 ." 731 in case a leap year is included
* throw error message
endif.     

Hope this helps,

Sajan Joseph.

Read only

Former Member
0 Likes
1,016
DATA: v_date LIKE RM06B-EEIND. Say this as D2
DATA: date LIKE sy-datum.
v_date = '01/01/2007'. D2
CALL FUNCTION 'DATE_IN_FUTURE'
  EXPORTING
    anzahl_tage                   = '24'
    import_datum                  = v_date
 IMPORTING
*   EXPORT_DATUM_EXT_FORMAT       =
   EXPORT_DATUM_INT_FORMAT       = date
          .
If ( D1 <=  date 
and D1 >= D2 ). ( Check if it is between D2 and date- 2years+D2 )
Message e000 with 'Error;.
ENDIF.

I will explain u teh logic if any mistakes are there check and change it.

The FM is used to get the next two years from the given date, that is y i have paseed 24 if u want previous 24 u can pass -24.

Then I am checking whether the date D1 is between the date D2 and the next two years, if so error message.

Reward if this helsp.

Read only

Former Member
0 Likes
1,015

Hi Gaurav,

use function Module "<b>HR_HK_DIFF_BT_2_DATES</b>" as follows :

********************************************************************************

parameters : d1 type sy-datum,

d2 type sy-datum.

data : years type i.

CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'

EXPORTING

DATE1 = d1

DATE2 = d2

OUTPUT_FORMAT = '07'

IMPORTING

YEARS = years

EXCEPTIONS

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

if years > 2.

**your error message

endif.

******************************************************************************************

here make sure that the date that u pass <b>DATE1 should be grater than the date DATE2</b>, otherwise it will raise an exception.

so Gaurav...u r done with this problem of yours ...cheers...

Reward points for useful answer.

Regards,

Tejas

Read only

Former Member
0 Likes
1,015

hi

good

try this function module

DATE_COMPUTE_DAY Returns a number indicating what day of the week the date falls on. Monday is returned as a 1, Tuesday as 2, etc.

DATE_IN_FUTURE Calculate a date N days in the future.

thanks

mrutyun^

Read only

Former Member
0 Likes
1,015

hai Gaurav try it....

DATA: DATE1 LIKE SY-DATUM,

DATE2 LIKE SY-DATUM,

DATE3 LIKE SY-DATUM.

DATE1 = SY-DATUM.

DATE2 = '20080203'.

DATE3 = DATE1.

ADD 2 TO DATE3+0(4). " Date after 2 years.

IF DATE2 BETWEEN DATE1 AND DATE3.

Error Message.......

ENDIF.