Dates at Midnight

B

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?
 
C

Chris Dunaway

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
 
C

Claes Bergefall

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
 
C

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
 
J

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


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

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.
 

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

Top