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

Time Problem

P: n/a
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?

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


P: n/a

"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:ON**************@TK2MSFTNGP14.phx.gbl...
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.
Nov 21 '05 #2

P: n/a
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.

"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
My Bad I meant to say 1AM....

If it says 1:00:00AM it still runs.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:e0**************@TK2MSFTNGP14.phx.gbl:
"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:ON**************@TK2MSFTNGP14.phx.gbl...
> 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.

Nov 21 '05 #3

P: n/a
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

"Imran Koradia" <no****@microsoft.com> wrote in message
news:uV**************@TK2MSFTNGP12.phx.gbl...
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.

"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
My Bad I meant to say 1AM....

If it says 1:00:00AM it still runs.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:e0**************@TK2MSFTNGP14.phx.gbl:
"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:ON**************@TK2MSFTNGP14.phx.gbl...
> 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.


Nov 21 '05 #4

P: n/a
My Bad I meant to say 1AM....

If it says 1:00:00AM it still runs.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:e0**************@TK2MSFTNGP14.phx.gbl:
"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:ON**************@TK2MSFTNGP14.phx.gbl...
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.


Nov 21 '05 #5

P: n/a
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

"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:ON**************@TK2MSFTNGP14.phx.gbl...
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?

Nov 21 '05 #6

P: n/a
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

Nov 21 '05 #7

P: n/a
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.

"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
My Bad I meant to say 1AM....

If it says 1:00:00AM it still runs.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:e0**************@TK2MSFTNGP14.phx.gbl:
"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:ON**************@TK2MSFTNGP14.phx.gbl...
> 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.

Nov 21 '05 #8

P: n/a
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

"Imran Koradia" <no****@microsoft.com> wrote in message
news:uV**************@TK2MSFTNGP12.phx.gbl...
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.

"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
My Bad I meant to say 1AM....

If it says 1:00:00AM it still runs.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:e0**************@TK2MSFTNGP14.phx.gbl:
"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:ON**************@TK2MSFTNGP14.phx.gbl...
> 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.


Nov 21 '05 #9

P: n/a
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

"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:ON**************@TK2MSFTNGP14.phx.gbl...
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?

Nov 21 '05 #10

P: n/a
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

Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.