Comparing dates

  • Thread starter Thread starter Nathan
  • Start date Start date
N

Nathan

Hi,

I can't seem to this part of my function to work:

Private Function TotalHours(ByVal InTime As Date, ByVal OutTime As Date) As
Double
Msgbox(InTime.Tostring)
Select Case InTime
Case #12:00:00 AM# To #8:00:59 AM#
etc., etc.
Case #8:01:00 AM# To #8:15:59 AM#
etc., etc.
End Select
End Function

The values passed to InTime and OutTime are gathered from a dataset in a
manner like CDate(CurRow("InTime")) where the "InTime" column stores a Date
variable. The messagebox gives: "Nov/08/2004 8:35:42 AM" The Select Case
is passed up completely, that is, none of the Case statements are evaluated
as true.

This function worked before when I had the date stored as a string. Now
that I have changed it to a date, it doesn't work. I'd appreciate any hints
to what might be going wrong.

Thanks,
Nathan
 
use DateTime.Parse on the string value and then you can compare it directly
as a date.
 
Nathan,
The reason this is failing is that InTime is a DateTime it has both a Date &
a Time component to it. You are attempting to compare the Date & Time value
to a simple Time Value. You need to remove the Date value from InTime so as
the compare will work.

However I would probably use a TimeRange object instead (see below).
Something like:

' I would consider more descriptive names
' such as FirstShift, SecondShift, ThirdShift.
Public Shared Readonly MidnightToEightAm As New TimeRange(#12:00:00
AM#, #8:00:59 AM#)
Public Shared Readonly EightAmToEightFifteen As New
TimeRange(#8:01:00 AM#, #8:15:59 AM#)
...
Private Function TotalHours(ByVal InTime As Date, ByVal OutTime As Date)
As Double
Msgbox(InTime.Tostring)

If MidnightToEightAm.Contains(InTime) Then
'etc., etc.
ElseIf EightAmToEightFifteen.Contains(InTime) Then
'etc., etc.
ElseIf ...
...
Else
...
End If
End Function

Depending on how you are using inTime & outTime, I would consider adding an
overload for the Contains method that check to see if one TimeRange
contained a second TimeRange (post if you would like a sample of such a
function), or passing inTime & outTime as a TimeRange also.


TimeRange: A TimeRange represents a range of time that can be easily checked
to see if a date/time value is contained within the range.

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

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

Hope this helps
Jay
 
Back
Top