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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find time in a given range...

Status
Not open for further replies.

Caradog

Programmer
Jun 28, 2000
73
0
0
GB
Maybe I'm just a little tired and the last pint is kicking in, but I'm a little lost on a time issue.

I have three time ranges:

1: 07:00 to 12:00
2: 12:00 to 19:00
3: 19:00 to 07:00

Then I have time value, lets say its 10:00 (but it will be others too) and I want to find which of the above range it falls into (obviously 1), but how do I code this? I'm having all sorts of fun with VB's time controls :-/
 
Hi There,

It's really late, so I'm not sure if this is going to work out of the box. I can't remember off the top of my head if the operators work on the Timespan objects, but try this out. You may have to use the Compare methods of the Timespans to test the values, but this should be along the lines of what you are looking for.

Private Function IsTimeBetween(ByVal tsToTest As TimeSpan, ByVal tsStart As TimeSpan, ByVal tsEnd As TimeSpan) As Boolean
Dim bRet As Boolean

bRet = False

If tsStart.Compare(tsEnd) = (tsStart > tsEnd) Then
Dim ts0 As TimeSpan
Dim ts1 As TimeSpan
ts0 = New TimeSpan(0) '00:00:00
ts0 = New TimeSpan(TimeSpan.TicksPerDay - 1) '23:59:59.xxx

bRet = (IsTimeBetween(tsToTest, tsStart, ts1) Or (IsTimeBetween(tsToTest, ts0, tsEnd)))
Else
If (tsToTest >= tsStart) And (tsToTest <= tsEnd) Then
bRet = True
End If
End If

Return bRet
End Function

Private Function IsTimeBetween(ByVal dtToTest As DateTime, _
ByVal tsStart As TimeSpan, ByVal tsEnd As TimeSpan) As Boolean
Return IsTimeBetween(dtToTest.TimeOfDay, tsStart, tsEnd)
End Function

HTH,

------------------------------------------
Steen Bray
Kermode Computing Solutions
 
I was more thinking in the line of

Code:
dim d as datetime
dim dhour as integer
d = cdate("05/05/1918 10:00:00")
if dhour > 7 and dhour < 12 then
end if
if dhour > 12 and dhour < 19 then
end if
if dhour > 19 and dhour < 7 then
end if

Christiaan Baes
Belgium

[b]If you want to get an answer read this FAQ faq796-2540[/b]
[i]There's no such thing as a winnable war - Sting[/i]
 
Hey chrissie

Good to know your last post is proof that even geniuses have a bad day sometimes.


Sweep
...if it works dont mess with it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top