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: 

Overlap of date range

Former Member
0 Kudos

Hi ,

I have a reqmnt of checking whether the two date ranges overlaps each other.

for e.g fromdate1 todate1 and fromdate2 todate2 should not intersect. Is there any FM for this, or can anyone help me with the logic please.

thanks

Points will be rewarded

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

You can check if a date is greater/ lesser than other date by If statements. It works for date, as it works for numeric values.

Eg

If date1 GT Date2 .

Do some thing.

Endif.

Regards

Srikanth

14 REPLIES 14

Former Member
0 Kudos

Hi

You can check if a date is greater/ lesser than other date by If statements. It works for date, as it works for numeric values.

Eg

If date1 GT Date2 .

Do some thing.

Endif.

Regards

Srikanth

0 Kudos

Hi Srikant, I guess my reqmnt is not so st fw.

Let me explain u with an e.g

1st date range: 01/10/2006 to 31/10/2006

2nd date range: 10/10/2006 to 10/11/2006

3rd date range: 15/09/2006 to 15/10/2006

4th date range: 12/10/2006 to 20/10/2006

5th date range 01/11/2006 to 15/11/2006

if you see 2nd 3rd and 4th overlaps with 1st.

but 5th doesnot overlap with 1st.

I gues i have to write different cases to validate it.

But a FM would have been an easier way.

0 Kudos

Hi Srikant,

As you have all the dates (From date, To date) in an internal table, sort the internal table with From date parameter. Then in the loop statement you can check whether the From date of nth record is less than the To Date of (n-1)th record. This way you can find out the overlap dates. Hope this will help u.

e.g itab (from_date, to_date)

sort itab by from_Date.

loop at itab.

if sy-tabix = '1'.

v_Date1 = itab-to_date.

else.

if itab-from_Date le v_date1.

HERE is Overlap Date

endif.

v_Date1 = itab-to_date.

endif.

endloop.

0 Kudos

Thanks for all ur replies. I got a fair idea about how to go about it. This is what i am using and is working fine. thought it might be useful to u too. If u have any comments let me know.

IF

( from_date between wa_tab-from_date and wa_Tab-to_date

OR to_Date between wa_tab-from_date and wa_Tab-to_date

or ( from_date < wa_tab-from_Date and to_date > wa_tab-to_date ) )

Then.. it overlaps.

0 Kudos

Dear Kantheri,

Your solution works of course very fine, but there is a simpler algorithm:


* Test date overlap
  IF  from_date LE wa_tab-to_date
  AND to_date   GE wa_tab-from_date.
*   It overlaps
*   ...
  ENDIF.

Actually this algorithm is used by SAP itself.

Regards,

Rob.

0 Kudos

Brilliant sure shot solution by Rob. Resolved my issue also, multiple validation scenarios resolved in a simple sureshot code. Simply outstanding.

Thanks & Regards,

FB

Former Member
0 Kudos

Hi,

Fill a range table with fromdate1 todate1 . Then you can simply check

ranges : rdate1 for sy-datum.

rdate1-low = 'romdate1'.

rdate-high = 'todate1'.

rdate1-sign = 'I'.

rdate1-option = 'EQ'.

append rdate1.

if fromdate2 in r_date1.

endif.

if todate2 in r_date1.

endif.

Hope this helps.

0 Kudos

Thanks Imtiaz, looks like this might work, or atleast gives me a way forward

Former Member
0 Kudos

Hi,

You can compare todate1 and fromdate2.

IF FROMDATE2 LT TODATE1.

  • Dates overlap

ENDIF.

This is assuming that fromdate1 < todate1 and fromdate2 < todate2.

Hope this helps!

Regards,

Saurabh

Former Member
0 Kudos

Check the below code:

REPORT ysample60 .

PARAMETERS: date_fr1 TYPE sy-datum OBLIGATORY,

date_to1 TYPE sy-datum OBLIGATORY,

date_fr2 TYPE sy-datum OBLIGATORY,

date_to2 TYPE sy-datum OBLIGATORY.

AT SELECTION-SCREEN.

From date is greater than End date - error

IF date_fr1 > date_to1.

MESSAGE e001(zspa) WITH 'Please enter a valid date'.

ENDIF.

From date is greater than End date - error

IF date_fr2 > date_to2.

MESSAGE e001(zspa) WITH 'Please enter a valid date'.

ENDIF.

IF date_fr1 > date_fr2.

IF date_to2 > date_to1.

MESSAGE e001(zspa) WITH 'Please enter a valid date'.

ELSEIF date_to2 = date_to1.

MESSAGE e001(zspa) WITH 'Please enter a valid date'.

ENDIF.

ELSEIF date_fr1 = date_fr2.

IF date_to2 = date_to1.

MESSAGE e001(zspa) WITH 'Please enter a valid date'.

ENDIF.

ELSEIF date_fr1 < date_fr2.

IF date_to2 = date_to1.

MESSAGE e001(zspa) WITH 'Please enter a valid date'.

ENDIF.

IF date_to1 BETWEEN date_fr2 AND date_to2.

MESSAGE e001(zspa) WITH 'Please enter a valid date'.

ENDIF.

ELSE.

IF date_to1 > date_to2.

MESSAGE e001(zspa) WITH 'Please enter a valid date'.

ENDIF.

ENDIF.

uwe_schieferstein
Active Contributor
0 Kudos

Hello

Here is my ABAP-OO approach using class CL_RECA_DATE_SLICES (Time Slot Management). The following sample report shows how it works:

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_TIME_SLICES
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_time_slices.

TYPE-POOLS: abap, icon.

DATA:
  gd_is_in_slice    TYPE abap_bool,
  gs_slice          TYPE recadaterange,
  gt_slices         TYPE re_t_recadaterange,
  go_date           TYPE REF TO cl_reca_date_slices.



SELECT-OPTIONS:
  o_time1      FOR syst-datum,
  o_time2      FOR syst-datum.


START-OF-SELECTION.


* Create instance
  CREATE OBJECT go_date
*    EXPORTING
*      IF_NO_COMPLETE_RANGE = ABAP_FALSE
      .

* Add first period to instance == current slice
  gs_slice-datefrom = o_time1-low.
  gs_slice-dateto   = o_time1-high.
  APPEND gs_slice TO gt_slices.
  go_date->set_slices( gt_slices ).


* Check if 2nd period is within first period
  CALL METHOD go_date->is_entry_in_current_slice
    EXPORTING
      id_datefrom = o_time2-low
      id_dateto   = o_time2-high
    RECEIVING
      rf_valid    = gd_is_in_slice.



  WRITE: / 'Period:', o_time2-low  DD/MM/YYYY, '-',
                      o_time2-high DD/MM/YYYY.


  IF ( gd_is_in_slice = abap_true ).
    WRITE: / icon_green_light AS ICON, 'is in'.
  ELSE.
    WRITE: / icon_red_light AS ICON, 'is not in'.
  ENDIF.

  WRITE: / 'Period:', o_time1-low  DD/MM/YYYY, '-',
                      o_time1-high DD/MM/YYYY.


END-OF-SELECTION.

The output for overlapping periods is:

Period: 02.02.2006 - 05.05.2006 
     is in                      
Period: 01.01.2006 - 03.03.2006

The output for non-overlapping periods is:

Period: 02.04.2006 - 05.05.2006 
     is not in                  
Period: 01.01.2006 - 03.03.2006

This class has several other methods that might be quite useful. However, I haven't tested them yet.

Regards

Uwe

Former Member
0 Kudos

Hi,

Try this..

PARAMETERS: P_FD1 TYPE SYDATUM OBLIGATORY.

PARAMETERS: P_TD1 TYPE SYDATUM OBLIGATORY.

PARAMETERS: P_FD2 TYPE SYDATUM OBLIGATORY.

PARAMETERS: P_TD2 TYPE SYDATUM OBLIGATORY.

AT SELECTION-SCREEN.

RANGES: R_DATE FOR SY-DATUM.

REFRESH: R_DATE.

R_DATE-SIGN = 'I'.

R_DATE-OPTION = 'BT'.

R_DATE-LOW = P_FD1.

R_DATE-HIGH = P_TD1.

APPEND R_DATE.

IF P_FD2 IN R_DATE OR

P_TD2 IN R_DATE.

MESSAGE E208(00) WITH 'DATES OVER LAP'.

ENDIF.

Thanks,

Naren

Peter_Inotai
Active Contributor
0 Kudos

Check this solution:

I had the same problem, I belive using ranges improves the code readability a lot.

Peter

Message was edited by: Peter Inotai

0 Kudos

Hi All,

You can use the FM to validate the date overlap when from date and to date in which the date ranges have been given.

<i> CALL FUNCTION <b>'GM_VALIDATE_DATE_RANGE'</b>

EXPORTING

i_from_date = sy-datum

i_to_date = c_enddt "'99991231'

TABLES

t_daterange = p_dates

EXCEPTIONS

ranges_overlap = 1

range_has_holes = 2

continuous_but_excessive = 3

  • OTHERS = 4

erro_message = 99.

CASE sy-subrc.

WHEN '0'.

WHEN '1'.

MESSAGE e004(z_common) DISPLAY LIKE 'E'

WITH 'Date range should not overlap with Existing One'(003).

WHEN '2'.

MESSAGE e004(z_common) DISPLAY LIKE 'E'

WITH 'Date range should not overlap with Existing One'(003).

ENDCASE.</i>

<b>OR</b>

Only for date ranges validation no matter whatever the date ranges are given.

you can use the Z function module from the above one mentioned.

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

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

<b><i>FUNCTION zgm_validate_date_range.

*"----


""Local Interface:

*" IMPORTING

*" REFERENCE(I_FROM_DATE) TYPE DATS OPTIONAL

*" REFERENCE(I_TO_DATE) TYPE DATS OPTIONAL

*" TABLES

*" T_DATERANGE STRUCTURE GMDATERANGE

*" EXCEPTIONS

*" RANGES_OVERLAP

*" RANGE_HAS_HOLES

*" CONTINUOUS_BUT_EXCESSIVE

*"----


DATA: i TYPE i,

l_next_date LIKE sy-datum,

l_first_date LIKE sy-datum,

l_last_date LIKE sy-datum,

no_days TYPE tfmatage,

cnt_next TYPE i,

w_daterange TYPE gmdaterange.

DESCRIBE TABLE t_daterange LINES i.

IF i > 1.

SORT t_daterange BY from_date.

  • First determine if the slices are continuous and have

  • no gaps.

LOOP AT t_daterange.

cnt_next = sy-tabix + 1.

IF sy-tabix > 1. " not first record

IF t_daterange-from_date <= l_next_date.

RAISE ranges_overlap.

ENDIF.

IF t_daterange-to_date <= l_next_date.

RAISE range_has_holes.

ENDIF.

ELSE.

  • save first date

MOVE t_daterange-from_date TO l_first_date.

ENDIF.

  • update end of range

MOVE t_daterange-to_date TO : l_last_date,

l_next_date.

ENDLOOP.

ENDIF.

ENDFUNCTION.</i></b>

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

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

Thanks

Ramesh Babu N