[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# How to check Date periods

Posted on 2005-04-11
Medium Priority
161 Views
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)

0
Question by:XARRISVAVLIS
• 3
• 2

LVL 12

Expert Comment

ID: 13758878
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
0

LVL 22

Accepted Solution

JesterToo earned 600 total points
ID: 13758964
Assuming that A is the earlier date in the A-B pair and that C is the earlier date in the C-D pair then...

If B < C Or D < A Then
' date ranges DO NOT overlap
Else
' date ranges DO overlap
End If

HTH,
Lynn
0

LVL 12

Expert Comment

ID: 13759219
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
0

LVL 22

Expert Comment

ID: 13764264
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
0

LVL 22

Expert Comment

ID: 13766504
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
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month19 days, 10 hours left to enroll