between two times

F

frank

Hello,

I am developing a vb.net app and need a little help with determing if the current time is between two times. Kind of like a Shift:
if current time is between the hours of 0701 and 1500, then user is in Shift one
else if current time is between the hours of 1501 and 2300, then user is in shift two.
Ditto for shift three.

How do I compare the time now() and determine if it falls between two different hours?
I have tried something along these lines:
if now.timeofday < 0700 AND now.timeofday < 1500 then
'Do my bidding
else...

This doesn't work as vb.net doesn't allow a greater than/less than comparison on a timespan?

Any Help would be greatly appreciated.

Ciao,

Frankie
--
 
A

Armin Zingler

Frank said:
Hello,

I am developing a vb.net app and need a little help with determing
if the current time is between two times. Kind of like a Shift: if
current time is between the hours of 0701 and 1500, then user is in
Shift one
else if current time is between the hours of 1501 and 2300, then
user is in shift two.
Ditto for shift three.

How do I compare the time now() and determine if it falls between
two different hours?
I have tried something along these lines:
if now.timeofday < 0700 AND now.timeofday < 1500 then
'Do my bidding
else...

This doesn't work as vb.net doesn't allow a greater than/less than
comparison on a timespan?

Any Help would be greatly appreciated.


Dim ts As TimeSpan

ts = Date.Now.TimeOfDay

If ts.Hours >= 7 AndAlso ts.Minutes <= 15 Then

- or -

Dim ts, start, [end] As TimeSpan

ts = Date.Now.TimeOfDay
start = New TimeSpan(7, 0, 0)
[end] = New TimeSpan(15, 0, 0)

If ts.Ticks >= start.Ticks AndAlso ts.Ticks <= [end].Ticks Then



Armin
 
M

Mike

Hai,
why don't you convert the timeofday to seconds by using some
multiplication like...

hours * 3600 + mins * 60 + seconds

And compare the times in seconds..

Just a thought..if nothing else works!
 
G

Gman

Blue Eyes,

I guess you're using VS2003 (cos you can use comparison operators in
VS2005).

Try using CompareTo. Seems to work for me:

Dim ts As New TimeSpan(13, 0, 0)
If DateTimePicker1.Value.TimeOfDay.CompareTo(ts) = -1 Then
Label1.Text = "Less"
Else
Label1.Text = "Greater or equal"
End If
 
F

frank

Armin,

Thanks for the response. I will give that a try and let you know.

Thanks again!

Frankie
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top