Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time Comparison

Status
Not open for further replies.

newmem

Programmer
Jul 23, 2002
35
US
Hi guys,

I am struggling with this Time comparison :

I need to check if the time range 10:00 PM - 10:15 PM falls between the range 09:59 PM - 12:02 AM.
I tried various ways to compare but no luck.

here's what my code looks like:
Assume T1 = 09:59 PM , T2 = 12:02 AM, tempT1=10:00 PM , tempT2 = 10:15 PM.

' check if T1 is between tempT1 & tempT2
If FormatDateTime(T1, DateFormat.ShortTime) <= FormatDateTime(tempT1 , DateFormat.ShortTime) And _
FormatDateTime(T2, DateFormat.ShortTime) >= FormatDateTime(tempT2, DateFormat.ShortTime) Then

'**** do something
end if

'check if tempT2 falls in T & t2.
If FormatDateTime(tempT2, DateFormat.ShortTime) > FormatDateTime(T1, DateFormat.ShortTime) And _
FormatDateTime(tempT2, DateFormat.ShortTime) <= FormatDateTime(T2, DateFormat.ShortTime) Then
'**** do something
end if


Can someone help? Appreciate it.



 
I messing with the coding, but the first problem I encountered was that 12:02am is less than 10:00pm.
 
This feels like a HACK, but it seems to work.

Dim Time1 As Date
Dim Time2 As Date
Dim TempTime1 As Date
Dim TempTime2 As Date

Time1 = CDate(#12/25/1977# & &quot; &quot; & T1.Text) '9:59pm
Time2 = CDate(#12/25/1977# & &quot; &quot; & T2.Text) '12:02am
TempTime1 = CDate(#12/25/1977# & &quot; &quot; & tempT1.Text) '10:00pm
TempTime2 = CDate(#12/25/1977# & &quot; &quot; & tempT2.Text) '10:15pm

If Time2 < Time1 Then
Time2 = CDate(#12/26/1977# & &quot; &quot; & T2.Text)
End If

If TempTime1 > Time1 And TempTime2 > Time1 And TempTime1 < Time2 And TempTime2 < Time2 Then
MsgBox(&quot;The Time is Within the alloted time span&quot;)
Else
MsgBox(&quot;Do your time figuring again DUDE!!!&quot;)
End If


Let me know if you come up with something better.

Kris
 
the date comparisons do return 12:02 am is less than 10:00 pm but this value is after midnight (date would be the next day).
I need to check in the code if 10 pm-10:15 pm is between the 9:59 pm-12:02am .

ignore the code I sent but is there any other way to do the compare? Fresh ideas would help.
 
Most likely you'll have to use a full datetime object so that you can add 1 day to T2.

T2 = T2.AddDay(1)

This is assuming that your variables are dateTime objects rather than strings.

On a side note if they are datetime objects you don't have to use the FormatDateTime function. Just use the ToShortTime method. Or as I mentioned you'll need a full date on that as well.

Not sure exactly how the range must fall in between the specified times. Can the upper or lower value be outside the specified times while the other is inside? The following example may help


Dim mFrom As DateTime = txtFrom.Text
Dim mTo As DateTime = txtTo.Text
Dim T1 As DateTime = &quot;09:59 PM&quot;
Dim T2 As DateTime = &quot;12:02 AM&quot;

T2 = T2.AddDays(1)

If (mFrom > T1 And mFrom < T2) And (mTo > T1 And mTo < T2) Then
'Range is good
Else
'Range bad
End If


Just remember that the addDays is only needed when the time crosses to tommorows time.

I am not sure that it can be done as us humans can understand it with the functions given you may need to write your own. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top