Link to home
Start Free TrialLog in
Avatar of XARRISVAVLIS
XARRISVAVLIS

asked on

How to check Date periods

Hello ..

Is there a way so i can see if a certain space between two DATES    e.g    ( A  to B)
is between another [C to D]

i have to check if       [C   ( A    D]     B)
                      OR           (A    [C      B)     D]
                      OR     [C   (A             B)     D]
                     OR            (A  [C D]    B)

Avatar of Bob Lamberson
Bob Lamberson
Flag of United States of America image

I'm assuming you are looking for overlapping periods of time.
If C < A And D > A And D < B Then
    'set var to true
ElseIf A < C And C < B And D > B Then
    'set var to true
ElseIf C < A And B < A And D > B Then
    'set var to true
ElseIf A < C And C < B And D < B Then
    'set var to true
Else
    'set var to false
End If

Bob
ASKER CERTIFIED SOLUTION
Avatar of JesterToo
JesterToo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jester Too
I think maybe that's over simplified:
If A < C  they could still overlap.
Might have to use B < C AND D < A , but I haven't checked it out.



Bob
Actually, if the assumptions I stated are correct, the simple comparison does produce the correct answer.  However, if we can't rely on the two date pairs being "ordered" then the following VBScript example provides a similarly simple comparison technique that demonstrates the same idea... that being:  if the latest date in the A-B pair precedes the earliest date in the C-D pair or the latest date in the C-D pair precedes the earliest date in the A-B pair then the dates cannot overlap, otherwise they do.

Dim a, b, c, d

a = cdate(wscript.arguments.item(0))
b = cdate(wscript.arguments.item(1))
c = cdate(wscript.arguments.item(2))
d = cdate(wscript.arguments.item(3))

Overlap a, b, c ,d

Sub Overlap(w, x, y, z)

   If Max(w, x) < Min(y, z) _
   Or Max(y, z) < Min(w, x) Then
      wscript.echo "Dates DO NOT overlap"
   Else
      wscript.echo "Dates DO overlap"
   End If

End Sub

function min( a, b )
  if a < b then
    min = a
  else
    min = b
  end if
end function

function max( a, b )
  if a > b then
    max = a
  else
    max = b
  end if
end function


-- Lynn
xarrisvavavlis...

Is there something wrong with the answer I provided?  If not, why the poor grade?  A "C" is the lowest grade that can be awarded.  If you get a reputation for giving out low grades like this you will find that very few people here at EE will be willing to respond to your questions in the future.

Regards,
Lynn