Home All Groups Group Topic Archive Search About
Author
27 Jun 2006 2:16 AM
Brian8568
I'm having a problem with VB 2005.  I'm trying to compare 2 dates as
follows:

If AddTo <= AddFrom Then
   'Error Code
Else
   'Happy Code
End If

AddFrom should be earlier than AddTo.

This works great unless the AddTo has a time in the 12:00 AM hour and
AddFrom has the same day as AddTo but a later time.

Example:

6/26/2006 12:00:01 AM      6/26/2006 11:45:00 PM - True
6/26/2006 12:00:01 AM      6/27/2006 11:45:00 PM - False
6/26/2006 1:00:00 AM        6/26/2006 11:45:00 PM - False
6/26/2006 1:00:00 AM        6/25/2006 11:45:00 PM - True

Is this a flaw in VB or in my logic?

Author
27 Jun 2006 3:14 PM
Chris Dunaway
Brian8***@gmail.com wrote:
Show quoteHide quote
> I'm having a problem with VB 2005.  I'm trying to compare 2 dates as
> follows:
>
> If AddTo <= AddFrom Then
>    'Error Code
> Else
>    'Happy Code
> End If
>
> AddFrom should be earlier than AddTo.
>
> This works great unless the AddTo has a time in the 12:00 AM hour and
> AddFrom has the same day as AddTo but a later time.
>
> Example:
>
> 6/26/2006 12:00:01 AM      6/26/2006 11:45:00 PM - True
> 6/26/2006 12:00:01 AM      6/27/2006 11:45:00 PM - False
> 6/26/2006 1:00:00 AM        6/26/2006 11:45:00 PM - False
> 6/26/2006 1:00:00 AM        6/25/2006 11:45:00 PM - True
>
> Is this a flaw in VB or in my logic?

How are the AddTo and AddFrom variables populated?  Do they come from a
database?  Can you be sure that they are not in 24 hour format?  Some
more detailed code might be useful for people to help.

>From your examples, it almost looks as if it is only comparing the date
component and not both the date and time. 

Chris
Author
27 Jun 2006 3:38 PM
Claes Bergefall
<Brian8***@gmail.com> wrote in message
Show quoteHide quote
news:1151374604.851660.309010@i40g2000cwc.googlegroups.com...
> I'm having a problem with VB 2005.  I'm trying to compare 2 dates as
> follows:
>
> If AddTo <= AddFrom Then
>   'Error Code
> Else
>   'Happy Code
> End If
>
> AddFrom should be earlier than AddTo.
>
> This works great unless the AddTo has a time in the 12:00 AM hour and
> AddFrom has the same day as AddTo but a later time.
>
> Example:
>
> 6/26/2006 12:00:01 AM      6/26/2006 11:45:00 PM - True
> 6/26/2006 12:00:01 AM      6/27/2006 11:45:00 PM - False
> 6/26/2006 1:00:00 AM        6/26/2006 11:45:00 PM - False
> 6/26/2006 1:00:00 AM        6/25/2006 11:45:00 PM - True

Is this the result you're seeing or the result you're expecting? Using ISO
format instead (which imo is a lot easier to work with) the above dates are:
2006-06-26 00:00:01  2006-06-26 23:45:00
2006-06-26 00:00:01  2006-06-27 23:45:00
2006-06-26 01:00:00  2006-06-26 23:45:00
2006-06-26 01:00:00  2006-06-25 23:45:00

So the expected result when doing <left colum> <= <right column> would be:
True
True
True
False


> Is this a flaw in VB or in my logic?

I think your logic is wrong

This code:
Dim b As Boolean
Dim AddTo As DateTime
Dim AddFrom As DateTime
AddTo = #6/26/2006 12:00:01 AM#
AddFrom = #6/26/2006 11:45:00 PM#
b = (AddTo <= AddFrom)
AddTo = #6/26/2006 12:00:01 AM#
AddFrom = #6/27/2006 11:45:00 PM#
b = (AddTo <= AddFrom)
AddTo = #6/26/2006 1:00:00 AM#
AddFrom = #6/26/2006 11:45:00 PM#
b = (AddTo <= AddFrom)
AddTo = #6/26/2006 1:00:00 AM#
AddFrom = #6/25/2006 11:45:00 PM#
b = (AddTo <= AddFrom)

produces the following result (in order):
True
True
True
False

   /claes
Author
27 Jun 2006 4:07 PM
Cor Ligthert [MVP]
Claes,

You are right, I am always strugling with the am/pm format, but it would be
even nicer if you would use the ISO in your code instead of the literal.

AddTo = New DateTime(2006,06,26,23,45,00)

:-)

Cor

Show quoteHide quote
"Claes Bergefall" <louplou@nospam.nospam> schreef in bericht
news:%23zqWb9fmGHA.1800@TK2MSFTNGP05.phx.gbl...
> <Brian8***@gmail.com> wrote in message
> news:1151374604.851660.309010@i40g2000cwc.googlegroups.com...
>> I'm having a problem with VB 2005.  I'm trying to compare 2 dates as
>> follows:
>>
>> If AddTo <= AddFrom Then
>>   'Error Code
>> Else
>>   'Happy Code
>> End If
>>
>> AddFrom should be earlier than AddTo.
>>
>> This works great unless the AddTo has a time in the 12:00 AM hour and
>> AddFrom has the same day as AddTo but a later time.
>>
>> Example:
>>
>> 6/26/2006 12:00:01 AM      6/26/2006 11:45:00 PM - True
>> 6/26/2006 12:00:01 AM      6/27/2006 11:45:00 PM - False
>> 6/26/2006 1:00:00 AM        6/26/2006 11:45:00 PM - False
>> 6/26/2006 1:00:00 AM        6/25/2006 11:45:00 PM - True
>
> Is this the result you're seeing or the result you're expecting? Using ISO
> format instead (which imo is a lot easier to work with) the above dates
> are:
> 2006-06-26 00:00:01  2006-06-26 23:45:00
> 2006-06-26 00:00:01  2006-06-27 23:45:00
> 2006-06-26 01:00:00  2006-06-26 23:45:00
> 2006-06-26 01:00:00  2006-06-25 23:45:00
>
> So the expected result when doing <left colum> <= <right column> would be:
> True
> True
> True
> False
>
>
>> Is this a flaw in VB or in my logic?
>
> I think your logic is wrong
>
> This code:
> Dim b As Boolean
> Dim AddTo As DateTime
> Dim AddFrom As DateTime
> AddTo = #6/26/2006 12:00:01 AM#
> AddFrom = #6/26/2006 11:45:00 PM#
> b = (AddTo <= AddFrom)
> AddTo = #6/26/2006 12:00:01 AM#
> AddFrom = #6/27/2006 11:45:00 PM#
> b = (AddTo <= AddFrom)
> AddTo = #6/26/2006 1:00:00 AM#
> AddFrom = #6/26/2006 11:45:00 PM#
> b = (AddTo <= AddFrom)
> AddTo = #6/26/2006 1:00:00 AM#
> AddFrom = #6/25/2006 11:45:00 PM#
> b = (AddTo <= AddFrom)
>
> produces the following result (in order):
> True
> True
> True
> False
>
>   /claes
>
Author
28 Jun 2006 12:06 AM
Jay B. Harlow [MVP - Outlook]
In addition to the other comments.

You are comparing 2 date & time values.

It sounds like you want to compare 2 date values.

Remove the Time values from the dates & compare away.

| If AddTo.Date <= AddFrom.Date Then

If you have 2 date time values, and want just the Time you can use
DateTime.TimeOfDay property.

' VS 2005 syntax
| If AddTo.TimeOfDay <= AddFrom.TimeOfDayThen



--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


<Brian8***@gmail.com> wrote in message
Show quoteHide quote
news:1151374604.851660.309010@i40g2000cwc.googlegroups.com...
| I'm having a problem with VB 2005.  I'm trying to compare 2 dates as
| follows:
|
| If AddTo <= AddFrom Then
|   'Error Code
| Else
|   'Happy Code
| End If
|
| AddFrom should be earlier than AddTo.
|
| This works great unless the AddTo has a time in the 12:00 AM hour and
| AddFrom has the same day as AddTo but a later time.
|
| Example:
|
| 6/26/2006 12:00:01 AM      6/26/2006 11:45:00 PM - True
| 6/26/2006 12:00:01 AM      6/27/2006 11:45:00 PM - False
| 6/26/2006 1:00:00 AM        6/26/2006 11:45:00 PM - False
| 6/26/2006 1:00:00 AM        6/25/2006 11:45:00 PM - True
|
| Is this a flaw in VB or in my logic?
|
Author
30 Jun 2006 2:38 AM
Brian8568
Thanks to everyone for thier help.  I miscast AddTo and AddFrom as
strings instead of DateTime so naturally they compared incorrectly.  It
works exactly as I want with the proper type.