time compare? should be simple

B

Brian

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
 
C

Cor Ligthert[MVP]

Brian,

Maybe it is more simple (at least for me, not somebody from an English
culture), to use New Datetime.

dim a (as DateTime for older versions) = new DateTime(now.Year,now.Month,
Now,Day,8,0,0)

If a < Now then it is behind 8 o'clcok

Cor
 
B

Brian

not really sure what you mean there.. I have dtmTime defined as datetime
and when I do immed on it.. is
#11/5/2008 9:44:07 PM# but in my code posted previously... this was
evaluating to > than midnight
 
A

Armin Zingler

Brian said:
not really sure what you mean there.. I have dtmTime defined as datetime
and when I do immed on it.. is
#11/5/2008 9:44:07 PM# but in my code posted previously... this was
evaluating to > than midnight


Isn't everything > than midnight?


Try comparing the TimeOfDay only (YourDateTimePicker.Value.TimeOfDay)


Armin
 
B

Brian

this is what i get using the timeofday....

Overload resolution failed because no accessible '>' can be called with
these arguments:

'Public Shared Operator >(t1 As Date, t2 As Date) As Boolean': Value of type
'System.TimeSpan' cannot be converted to 'Date'.

'Public Shared Operator >(t1 As System.TimeSpan, t2 As System.TimeSpan) As
Boolean': Value of type 'Date' cannot be converted to 'System.TimeSpan'.



If dtmTime > #11:59:59 PM# And dtmTime < #08:00# Then

I would like for that to be true if the dtmTime is between those times..
otherwise false... dtmTime is defined as DateTime
 
O

OmegaSquared

Hello, Brian,

I'm a bit confused about what you are trying to do.

Re: "...formated for just time,"

As far as I know, the value returned by the DateTimePicker will include the
date even if the display is formatted to show only time. Perhaps your
problem is that you still need to truncate that date.

Re: "dtmTime > #11:59:59 PM# and dtmTime < #08:00:00 AM#"

You say this evaluates to True. But I think that there are not many (by
that I mean any) times for which the above statement would evaluate to True.
Don't you just want the second half of that statement. I.e.: "dtmTime <
#08:00:00 AM#"

Cheers,
Randy
 
C

Cor Ligthert[MVP]

I thought you did want every time behind 8 0'clock today.

But It is simple from this code that it goes only for today

I had first
dim b = new DateTime(New DateTime(now.Year,now.Month, Now,Day, 24,0,0)

dim a (as DateTime for older versions) = new DateTime(now.Year,now.Month,
Now,Day,8,0,0)

If a < Now and also Now < B then it is between 8 and 24 o'clock

Cor
 
A

Armin Zingler

Brian said:
this is what i get using the timeofday....

Overload resolution failed because no accessible '>' can be called
with these arguments:

'Public Shared Operator >(t1 As Date, t2 As Date) As Boolean': Value
of type 'System.TimeSpan' cannot be converted to 'Date'.

'Public Shared Operator >(t1 As System.TimeSpan, t2 As
System.TimeSpan) As Boolean': Value of type 'Date' cannot be
converted to 'System.TimeSpan'.



If dtmTime > #11:59:59 PM# And dtmTime < #08:00# Then

I would like for that to be true if the dtmTime is between those
times.. otherwise false... dtmTime is defined as DateTime

If dtmTime is a DateTime object, the last line should be compilable.
#11:59:59 PM# is actually 01/01/0001 11:59:59 PM and
#08:00# is actually 01/01/0001 08:00 AM

However, you want to compare times, not points in time. A TimeSpan object is
usually used to represent the time of a day, like the TimeOfDay property of
a DateTime object shows. It's the timespan from midnight until the time
given.

So, dtmTime should be a TimeSpan variable. Then the statement is:

If dtmTime > New TimeSpan AndAlso dtmTime < New TimeSpan(8, 0, 0) Then

Instead of
dtmTime > New TimeSpan
you can also use
dtmTime.Ticks > 0
or
dtmTime > New Timespan(0)
or
...


I don't know if midnight is to be included or excluded. It it's included,
the first expression is not required at all because ever time is equal to or
after midnight.


Armin
 
B

Brian

ok, i guess i am just not getting.. let me rephase what I am doing and you
tell me what is best to here
i have a function that accepts to varibles.. dtmdate as date, dtmTime as
date
Public Function GetShift(ByVal dtmDate As Date, ByVal dtmTime As DateTime)
As String



If dtmTime > #11:59:59 PM# And dtmTime < ShiftEndTime Then

strShift = "N"

dtmDate.AddDays(-1)



End If

return getshift(dtmDate, "N")

Now, if this function is ran between midnight and 8 am then I subtract 1 day
to get the team that started its night shift the previous day.
Not sure i am having a hard time understanding.. maybe i need a
vacation...lol
 
O

OmegaSquared

Hello, Brian,

Re: "well no, I want any time that is between midnight and 8am to be true..."

Ummm.... and can you tell me for what time between midnight and 8am the
result of:
dtmTime < #08:00:00 AM#

would not be True? The only one that comes to my mind is midnight itself.
So if you want your interval to exclude midnight, then try:

dtmTime > #00:00:00 AM# AND dtmTime < #08:00:00 AM#

Cheers,
Randy
 
S

Stephany Young

Yes, you are not getting it!

Firstly, when you post, please use appropriate punction and grammar. This
will make your posting a lot easier for to understand.

A variable of type DateTime represents a point in time. A point in time
always comprises both a date and a time of day.

If you populate such a variable with only a time part (say 8:00 AM) then the
value of the variable will be the system date combined with the time that
you specified, e.g.:

Dim MyVar = DateTime.Parse("08:00")

Console.Writeline(MyVar)

will result in #11/7/2008 8:00 AM#, or something like that, depending on
your system locale and your regional settings.

Obversely, if you populate such a variable with only a date part then the
value of the variable will be the date you specified combined with midnight,
e.g.:

Dim MyVar = DateTime.Today

Console.Writeline(MyVar)

will result in #11/7/2008 12:00 AM#, or something like that, depending on
your system locale and your regional settings.

An important point that you MUST ALWAYS remember is that midnight (12:00 AM)
is at the BEGINNING of the day and not at the end of the day.

If, as it appears, you want to know if any given point in time is prior to
8:00 AM today then you have a number of options:

If MyPointInTime < DateTime.Today.AddHours(8) Then
' MyPointInTime is prior to 8:00 AM today
End If

If MyPointInTime.Date = DateTime.Today AndAlso MyPointInTime.Hour < 8
Then
' MyPointInTime is prior to 8:00 AM today
End If

If DateTime.Today.AddHours(8).Subtract(MyPointInTime).TotalHours > 8
' MyPointInTime is prior to 8:00 AM today
End If

The options shown here are, by no means, definitive.

I strongly recommend that you review and experiment with the properties and
methods of the DateTime and TimeSpan types so that you become conversant
with what they represent and how they operate.
 
J

James Hahn

For the line
If dtmTime > #11:59:59 PM# And dtmTime < ShiftEndTime Then
to work it would have to somehow be interpreted as

If dtmTime is greater than '11:59:59 PM yesterday' and is also less than
'ShiftEndTime today' then ...

and I can't see anything in your code that enables that interpretation.

If that really was a time object you were comparing (which I believe it
isn't) then surely the > comparison should be to 00:00:00.
 
C

Cor Ligthert[MVP]

Brian.

As you look at Stephany's correct reply, then there is one different with my
code.

I have written that the time today between 8 and 24 o'clock, is not the
time from 00 to 08 o'clock

So there is something different. Is exactly 8 o'clock including what your
want or excluded from what you want.

Cor
 
B

Brian

8 would be the start of the next shift, I will try stephany's sample and i
need to play around with the timespan.. and ticks... and read more on the
msdn.
Thanks
 
B

Brian

Firstly, thanks for you explanation, I will try and play around with it....
Secondly, I didn't realize i was being graded for english composition...
 
B

Branco Medeiros

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

Brian

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

Armin Zingler

Brian said:
ok,, this is what I came up with... thanks for all the help

There's a lot I would change. see inline

Module Module1

Public ShiftStartTime As Date = New DateTime(Today.Year,
Today.Month, Today.Day, 6, 59, 59)

It's not very probable but as you retrieve the current date three times, the
date might change between the calls. I'd get the date only once. If you want
"today at 6:59:59 am", you can write

ShiftStartTime = Date.Today.Add(New TimeSpan(6, 59, 59))
Public ShiftEndTime As Date = New DateTime(Today.Year, Today.Month,
Today.Day, 18, 59, 59)

see above

Sub Main()

Console.WriteLine(testme(Today, Now))

Console.Read()

End Sub

Private Function testme(ByVal dtmdate As Date, ByVal dtmTime As
Date) As String

If dtmTime contains a time only, use a TimeSpan object.

ShiftEndTime = Date.Parse(CStr(#6:59:59 PM#))

Don't convert to String and back to Date. See solution above.

Dim a = New DateTime(dtmdate.Year, dtmdate.Month, dtmdate.Day, 0, 0,
0)

shorter:
a = dtmdate.date

'Dim b = New DateTime(dtmdate.Year, dtmdate.Month, dtmdate.Day, 6,
59, 59)

Dim Ishift As Date = CDate(dtmdate.ToShortDateString & " " &
dtmTime.ToShortTimeString)

Why two conversions again? Declare dtmTime as Timespan and you can simply
write

Dim Ishift = dtmDate.Add(dtmTime)

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

I didn't check the logic because it's still not clear to me what you are
trying to achieve.


Armin
 
B

Brian

Armin Zingler said:
There's a lot I would change. see inline



It's not very probable but as you retrieve the current date three times,
the date might change between the calls. I'd get the date only once. If
you want "today at 6:59:59 am", you can write

ShiftStartTime = Date.Today.Add(New TimeSpan(6, 59, 59))
This can not be done with option strict on?
 

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