Time Problem

S

scorpion53061

What I thought would be pretty easy has turned out not to be.

I have three variables. Actiontime is formatted as 08/11/2004 11:03PM

Actiontime
JobStreamStart (11:00:00PM)
JobStreamEnd (2:00:00AM)

If (Format(CDate(ActionTime), "t") >= JobStreamStart AND
(Format(CDate(ActionTime), "t") < jobstreamend Then

Do these actions

End If

Does not work. I am having a problem after midnight when it seems 4AM is
still not available for running. Does anyone have a suggestion?
 
I

Imran Koradia

scorpion53061 said:
What I thought would be pretty easy has turned out not to be.

I have three variables. Actiontime is formatted as 08/11/2004 11:03PM

Actiontime
JobStreamStart (11:00:00PM)
JobStreamEnd (2:00:00AM)

If (Format(CDate(ActionTime), "t") >= JobStreamStart AND
(Format(CDate(ActionTime), "t") < jobstreamend Then

Do these actions

End If

Does not work. I am having a problem after midnight when it seems 4AM is
still not available for running. Does anyone have a suggestion?

That's because 4 AM is not between 11PM and 2AM. Here's the way to go:

Private Sub testsub()
Dim ActionTime As DateTime = _
DateTime.Parse("08/11/2004 11:03PM")
testtime(ActionTime)
ActionTime = DateTime.Parse("08/12/04 04:00AM")
testtime(ActionTime)
End Sub

Private Sub testtime(ByVal ActionTime As DateTime)
Dim StartTime As DateTime = DateTime.Parse("08/11/04 11:00PM")
Dim EndTime As DateTime = DateTime.Parse("08/12/04 02:00AM")
If (DateTime.Compare(ActionTime, StartTime) >= 0 AndAlso _
DateTime.Compare(ActionTime, EndTime) < 0) OrElse _
(DateTime.Compare(ActionTime, StartTime) < 0 OrElse _
DateTime.Compare(ActionTime, EndTime) >= 0) Then
MessageBox.Show("Do These Actions..")
End If
End Sub

or modifying your code:

If ((Format(CDate(ActionTime), "t") >= JobStreamStart AndAlso _
(Format(CDate(ActionTime), "t") < jobstreamend)) OrElse _
((Format(CDate(ActionTime), "t") < JobStreamStart OrElse _
(Format(CDate(ActionTime), "t") >= jobstreamend)) Then
MessageBox.Show("Do These Actions..")
End If

hope that helps..
Imran.
 
I

Imran Koradia

In that case, only comparing time values wont work since its going to
compare the time values for a given date; so, 1AM for say today is not
between 11PM and 4AM of today. You'll need to use the date values -
8/12/2004 1AM is between 8/11/2004 11PM and 8/12/2004 4AM.You should use the
DateTime.Compare function to correctly compare such values.

hope that helps..
Imran.
 
D

Doug Bell

You could compare the just the time values but you would need to test to see
if your start time (JobStreamStart) is less than your end time for one
comparison

dim fDoActions as boolean
If JobStreamStart<=JobStreamEnd
fDoActions=(Format(CDate(ActionTime), "t") >= JobStreamStart AND
(Format(CDate(ActionTime), "t") < jobstreamend
else
fDoActions= not (Format(CDate(ActionTime), "t") < JobStreamStart OR
not (Format(CDate(ActionTime), "t") >= jobStreamEnd Do these actions
endif
If fDoActions Then
Do these actions
Endif
 
J

Jay B. Harlow [MVP - Outlook]

Scorpion,
I would use a TimeRange object.

http://www.martinfowler.com/ap2/range.html

Something like:

Public Structure TimeRange

' Store the range as TimeSpans as the comparisons are "easier"
Private ReadOnly m_start As TimeSpan
Private ReadOnly m_finish As TimeSpan

' used to handle special case of the range spanning midnight
Private ReadOnly m_midnight As Boolean

Public Sub New(ByVal start As DateTime, ByVal finish As DateTime)
m_start = start.TimeOfDay
m_finish = finish.TimeOfDay
m_midnight = (TimeSpan.Compare(m_start, m_finish) > 0)
End Sub

Public ReadOnly Property Start() As DateTime
Get
Return DateTime.MinValue.Add(m_start)
End Get
End Property

Public ReadOnly Property Finish() As DateTime
Get
Return DateTime.MinValue.Add(m_finish)
End Get
End Property

Public Function Contains(ByVal value As DateTime) As Boolean
Dim timeOfDay As TimeSpan = value.TimeOfDay
If m_midnight Then
Return TimeSpan.Compare(m_start, timeOfDay) <= 0 OrElse
TimeSpan.Compare(timeOfDay, m_finish) <= 0
Else
Return TimeSpan.Compare(m_start, timeOfDay) <= 0 AndAlso
TimeSpan.Compare(timeOfDay, m_finish) <= 0
End If
End Function

End Structure

Public Shared Sub Main()

Dim jobStreamRange As New TimeRange(#11:00:00 PM#, #2:00:00 AM#)

Dim actionTime As DateTime = #8/11/2004 11:03:00 PM#

If jobStreamRange.Contains(actionTime) Then
' Do these actions
End If

End If

Hope this helps
Jay
 
C

Cor Ligthert

Kelly,

Strangly enough Jay, does not shows it in is sample while we are normally
writting the same about this.

Because you are probably only working for US and English speaking Canada,
you can use the notation in those countries to create dates, however in my
opinion it is nicer to use the New Date to create dates.

You see it in the Fowler sample that is in Jays message. When you start
typing it, it shows directly as intelisence. New Date(year,month,etc)

All was it only for people in this newsgroup who are not from those
countries, who probably as me have to check that US&EnglishCanada system
twice.

:)

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

Similar Threads


Top