ok,, this is what I came up with... thanks for all the help
Module Module1
Public ShiftStartTime As Date = New DateTime(Today.Year, Today.Month,
Today.Day, 6, 59, 59)
Public ShiftEndTime As Date = New DateTime(Today.Year, Today.Month,
Today.Day, 18, 59, 59)
Sub Main()
Console.WriteLine(testme(Today, Now))
Console.Read()
End Sub
Private Function testme(ByVal dtmdate As Date, ByVal dtmTime As Date) As
String
ShiftEndTime = Date.Parse(CStr(#6:59:59 PM#))
Dim a = New DateTime(dtmdate.Year, dtmdate.Month, dtmdate.Day, 0, 0, 0)
'Dim b = New DateTime(dtmdate.Year, dtmdate.Month, dtmdate.Day, 6, 59, 59)
Dim Ishift As Date = CDate(dtmdate.ToShortDateString & " " &
dtmTime.ToShortTimeString)
Dim result As String = "Brian this is NOT between times " & a & " and " &
ShiftEndTime & " am....."
If (Ishift >= a) AndAlso (Ishift <= ShiftEndTime) Then
result = "Brian this is between times " & a & " and " & ShiftEndTime & "
am... "
End If
Return result
End Function
End Module
Brian said:
I have a datetimepicker formated for just time, the user selects the time.
I want to compare if that time is between midnight and 8 am
dtmTime > #11:59:59 PM# and dtmTime < #08:00:00 AM#
this evaluates to true when the time is not greater than
dtmTime > #9:32:34 PM#
Why is that? and How can I get this to work?
Brian
Hi,
Would you mind posting more information, prefereably a few lines of
your code where the problem happens? I tried to reproduce the
situation here, but it wasn't possible, i.e. I couldn't find any
situation where the test you propose would result true.
On the contrary, it seems to me that there isn't a value that would be
at the same time greater than #11:59:59 PM# and lower than #08:00:00
AM#... =D
A few things that come to mind, though:
a) As others pointed out, any time will be *after* midnight, therefore
there isn't much sense in testing for that. Conversely, no value
*today* will be after #11:59:59 PM#, cause then it will be *tomorrow*,
not today.
b) specifying just time literals, as you are doing (at least according
to the text in your post) isn't reliable for the kind of test you are
performing. For instance, #11:59:59 PM# in my system refers to #1/1/1
11:59:59 PM#, and #08:00:00 AM# refers to #1/1/1 08:00:00 AM#. The
value returned by the DateTimePicker, on the other hand, is bound to a
specific date (the current date, by default). You can't get more
'oranges to apples' than this, it seems (unless your system's date is
1/1/1, which I doubt... =)))
c) As Stephany points out (in the dreaded "please use appropriate
punction(sic) and grammar" post -- I'm trying hard to get my punction
right, here =)), there are several ways you can get what you want. To
me (who am known to be a little twisted in the logic department) the
solution would be in the lines of:
<example>
Dim ShiftStart As Date = Date.Today.Add(ShiftStartTime)
Dim ShiftEnd As Date = Date.Today.Add(ShiftEndTime)
If (dmtTime >= ShiftStart) _
AndAlso (dmtTime <= ShiftEnd) Then
' dmtTime is in the current shift
'...
</example>
In the example, appropriate time spans for ShiftStartTime and
ShifEndTime would be provided. In your case:
Dim ShiftStartTime As Timespan = TimeSpan.Parse("00:00:00")
Dim ShiftEndTime As TimeSpan = TimeSpan.Parse("07:59:59")
Hope this helps,
Regards,
Branco.