Skip to Content
avatar image
Former Member

Overlap of date range

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

7 Answers

  • Best Answer
    avatar image
    Former Member
    Oct 02, 2006 at 09:43 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

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

      Thanks & Regards,

      FB

  • avatar image
    Former Member
    Oct 02, 2006 at 07:58 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 02, 2006 at 09:51 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 02, 2006 at 10:15 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 02, 2006 at 08:10 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 02, 2006 at 09:44 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 04, 2006 at 09:15 AM

    Check this solution:

    How to check date overlapping?

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

    Peter

    Message was edited by: Peter Inotai

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      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