By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 2,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

Time Range Calculation?

P: n/a
I'm trying to find out if now() is between 9:00 AM and 5:00 PM I've tried
a few things but can't get it to work.

Thanks
Nov 21 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
In message <uA**************@TK2MSFTNGP14.phx.gbl>, Bdog
<bd***@Hotmail.com> writes
I'm trying to find out if now() is between 9:00 AM and 5:00 PM I've tried
a few things but can't get it to work.


Have you tried:

IF Hour(Now) >= 9 AND Hour(Now) <= 17 THEN
' Do Something
END IF
--
Andrew D. Newbould E-Mail: ne********@NOSPAMzadsoft.com

ZAD Software Systems Web : www.zadsoft.com
Nov 21 '05 #2

P: n/a
Thanks I'll try it, any idea on how to handle it if I want from 8:30 to
4:30?

Thanks

Barclay

"Andrew D. Newbould" <ne********@NOzadSPANsoft.com> wrote in message
news:uo**************@zadsoft.gotadsl.co.uk...
In message <uA**************@TK2MSFTNGP14.phx.gbl>, Bdog
<bd***@Hotmail.com> writes
I'm trying to find out if now() is between 9:00 AM and 5:00 PM I've trieda few things but can't get it to work.


Have you tried:

IF Hour(Now) >= 9 AND Hour(Now) <= 17 THEN
' Do Something
END IF
--
Andrew D. Newbould E-Mail: ne********@NOSPAMzadsoft.com

ZAD Software Systems Web : www.zadsoft.com

Nov 21 '05 #3

P: n/a
On 2005-06-08, Bdog <bd***@Hotmail.com> wrote:
Thanks I'll try it, any idea on how to handle it if I want from 8:30 to
4:30?

Thanks

Barclay

Barclay, look at the DateTime objects compare method...

Dim begin As New Date _
(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 30, 0)
Dim end As New Date _
(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 16, 30, 0)

Dim currentTime As Date = Date.Now ()
If Date.Compare (currentTime, begin) >= 0 And _
Date.Compare (currentTime, end) <= 0 Then
' Do Cool Stuff
End If

If you don't want it to be inclusive, then remove the equal signs :)
--
Tom Shelton [MVP]
Nov 21 '05 #4

P: n/a
Bdog,
I'm trying to find out if now() is between 9:00 AM and 5:00 PM I've
tried
a few things but can't get it to work.

I take for this the approach, that I first take the ticks from those values
and than compare those.

In my opinion is that the most easy way doing this.

I hope this helps,

Cor
Nov 21 '05 #5

P: n/a
In message <u0**************@TK2MSFTNGP14.phx.gbl>, Cor Ligthert
<no************@planet.nl> writes
Bdog,
I'm trying to find out if now() is between 9:00 AM and 5:00 PM I've
tried
a few things but can't get it to work.

I take for this the approach, that I first take the ticks from those values
and than compare those.

In my opinion is that the most easy way doing this.

I hope this helps,

Cor


Or just look at the other Date functions like Minute, Seconds, Month,
Day, Year etc.
--
Andrew D. Newbould E-Mail: ne********@NOSPAMzadsoft.com

ZAD Software Systems Web : www.zadsoft.com
Nov 21 '05 #6

P: n/a
Andrew,

Or just look at the other Date functions like Minute, Seconds, Month, Day,
Year etc.


Than you have to compare every part indivially with the ticks you do it in
one time.

See this sample that I made for this question.

\\\
Dim datenow As DateTime = Now
Dim datefive As DateTime = _
New DateTime(Now.Year, Now.Month, Now.Day, 17, 0, 0)
Dim datenine As DateTime = _
New DateTime(Now.Year, Now.Month, Now.Day, 9, 0, 0)
If datenow.Ticks < datefive.Ticks AndAlso _
datenow.Ticks > datenine.Ticks Then
MessageBox.Show("The time is between nine and five")
End If
///

Although I find your sample using the Microsoft.VisualBasic namespace very
nice, does this go as well for minutes or even seconds.

Therefore if the problem is exactly as it is. I would go for your sample.
This ticks answer I write forever because it gives never problems. (For the
sample from Tom this is only an alternative showed because of the sentence
before )

Cor
Nov 21 '05 #7

P: n/a
Thanks for all your help guys, I'll give it a shot.

Barclay

"Cor Ligthert" <no************@planet.nl> wrote in message
news:Oh**************@tk2msftngp13.phx.gbl...
Andrew,

Or just look at the other Date functions like Minute, Seconds, Month, Day, Year etc.

Than you have to compare every part indivially with the ticks you do it in
one time.

See this sample that I made for this question.

\\\
Dim datenow As DateTime = Now
Dim datefive As DateTime = _
New DateTime(Now.Year, Now.Month, Now.Day, 17, 0, 0)
Dim datenine As DateTime = _
New DateTime(Now.Year, Now.Month, Now.Day, 9, 0, 0)
If datenow.Ticks < datefive.Ticks AndAlso _
datenow.Ticks > datenine.Ticks Then
MessageBox.Show("The time is between nine and five")
End If
///

Although I find your sample using the Microsoft.VisualBasic namespace very
nice, does this go as well for minutes or even seconds.

Therefore if the problem is exactly as it is. I would go for your sample.
This ticks answer I write forever because it gives never problems. (For

the sample from Tom this is only an alternative showed because of the sentence before )

Cor

Nov 21 '05 #8

P: n/a
Bdog,
I defined a TimeRange type that is useful to check to see if a DateTime
falls within a certain Time Range (for example between 8AM & 5PM). See:

http://groups-beta.google.com/group/...9aa87555043055

Using TimeRange you could do something like:

Static workHours As New TimeRange(#8:00:00 AM#, #5:00:00 PM#)

If workHours.Contains(DateTime.Now) Then

End If

Hope this helps
Jay
"Bdog" <bd***@Hotmail.com> wrote in message
news:uA**************@TK2MSFTNGP14.phx.gbl...
| I'm trying to find out if now() is between 9:00 AM and 5:00 PM I've
tried
| a few things but can't get it to work.
|
| Thanks
|
|
Nov 21 '05 #9

P: n/a
"Bdog" <bd***@Hotmail.com> wrote in message
news:uA**************@TK2MSFTNGP14.phx.gbl...
I'm trying to find out if now() is between 9:00 AM and 5:00 PM


Format them all up as 24-hour times and compare - something like

sStart = dtStart.ToString( "HH:mm" )
sEnd = dtEnd.ToString( "HH:mm" )
sNow = Now().ToString("HH:mm" )

Select Case sNow
Case sStart To sEnd
' Yep!
Case Else
End Select

HTH,
Phill W.
Nov 21 '05 #10

P: n/a
Jay,

Very nice (it seems that I forget this one forever while I have seen this
more times from you), I even wrote once that I would find it even nicer when
ISO times where used.

However, why static?

Cor
Nov 21 '05 #11

P: n/a
Cor,
| However, why static?

Static workHours As New TimeRange(#8:00:00 AM#, #5:00:00 PM#)

I defined workHours as Static, as I view it effectively as a Constant.

Depending on the usage I would consider making it Shared Readonly in the
enclosing type.
| I even wrote once that I would find it even nicer when
| ISO times where used.
Agree: I prefer SQL's (not SQL Server) Date, Time, TimeStamp, & Interval
types to be more flexible over .NET's DateTime & TimeSpan types. For example
definitions:

http://www.sql.org/sql-database/post...-datetime.html
I've considered creating a library that implements Date, Time, Timestamp, &
Interval, but todate have not. (With SQL Server 2005 & VB 2005 one could
define SQL Server & .NET friendly versions). Implementing them interms of
DateTime & TimeSpan might be "easiest".

Date = DateTime without any Time value
Time = DateTime without any Date value
Timestamp = DateTime
Interval = TimeSpan

Of course with VB's Date keyword meaning DateTime, it might make using Date
to mean a real date awkward...

Hope this helps
Jay

"Cor Ligthert" <no************@planet.nl> wrote in message
news:es**************@TK2MSFTNGP10.phx.gbl...
| Jay,
|
| Very nice (it seems that I forget this one forever while I have seen this
| more times from you), I even wrote once that I would find it even nicer
when
| ISO times where used.
|
| However, why static?
|
| Cor
|
|
Nov 21 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.