How to Check time is between values?

Drache

Active member
Joined
Apr 8, 2006
Messages
26
Programming Experience
Beginner
Can someone please tell me how to check if a time is between two other times.

Time a: 15:00 is between time b: 14:00 and time c: 16:00

In addition, can you use time in a for loop

for a = 09:00 to 22:00 step 01:00

Thankyou.
 
try this:

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] t1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TimeSpan = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2]("01.01.2005 01:30").TimeOfDay
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] t2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TimeSpan = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2]("01.03.2005 02:30").TimeOfDay
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] t3 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TimeSpan = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2]("01.05.2005 05:30").TimeOfDay
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] t2.Subtract(t1).TotalMinutes > 0 [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] t3.Subtract(t2).TotalMinutes > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]MessageBox.Show("t2 is greater then t1 and lower then t3")
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i = t1.TotalHours [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] t2.TotalHours [/SIZE][SIZE=2][COLOR=#0000ff]Step[/COLOR][/SIZE][SIZE=2] 1
MessageBox.Show("Hour " & i)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]
 
Thankyou

Thankyou all for your help.:)

I used the code supplied and a DiffDate statement and everything worked great
 
Back
Top