Time Range Calculation?

  • Thread starter Thread starter Bdog
  • Start date Start date
B

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.

Thanks
 
Bdog said:
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
 
Thanks I'll try it, any idea on how to handle it if I want from 8:30 to
4:30?

Thanks

Barclay
 
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 :)
 
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
 
Cor Ligthert said:
Bdog,

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,
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
 
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/microsoft.public.dotnet.languages.vb/msg/809aa87555043055

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


| 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
|
|
 
Bdog said:
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.
 
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
 
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/postgresql/manual/datatype-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

| 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
|
|
 

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

Back
Top