473,385 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Dates at Midnight

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?

Jun 27 '06 #1
5 1344
Br*******@gmail.com wrote:
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

Jun 27 '06 #2
<Br*******@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.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
Jun 27 '06 #3
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

"Claes Bergefall" <lo*****@nospam.nospam> schreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
<Br*******@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.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

Jun 27 '06 #4
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
<Br*******@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.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?
|
Jun 28 '06 #5
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.

Jun 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas Scheiderich | last post by:
I have the following page as test.aspx: *************************************************** <html> <head> <title>Hello and Welcome Page</title> </head> <body> <center> <% Dim CurrentDate As...
3
by: kmcnet | last post by:
Hello Everyone and thanks for your help in advance. I am developing an application that utilizes a separate class library for data access to a SQL Server database. One of the tables has several...
2
by: wireless200 | last post by:
I've got a table with some datetime fields in it. One field (call it field 1) is of the form mm/dd/yyyy and the other two (fields 2 and 3) are in the form of hh:mm:ss:xx where xx is hundreths of...
12
by: Joseph Numpty | last post by:
Hello everyone. My first post... I'd like to add an automatically updating date field to a webpage. I found the below example on the internet which works brilliantly except I'd like an ordinal...
9
by: GGerard | last post by:
Using Access2000 I have an application that is running 24/7 and I would like this application to perform some routine housekeeping task at midnight. What would be an efficient way of...
4
by: JP | last post by:
I have a WS that returns a dataset. When I serialize the data and display in on the screen my dates always seem to be -1 or +1 of the date I exspected. 1/11/2005 10:00:00 PM where should have...
6
by: Simon Harvey | last post by:
Hi everyone, I need to be able to compare to dates to ensure that one is at least 1 day greater than the other. Im trying to do if(toDate !> fromDate){ // handle }
1
by: stormcandi | last post by:
Hello all, I am creating a web page in c# to allow a user to select a date from a calendar and then enter a start and end time. The problem I am having is when I have a start time of 10:00 PM and...
4
by: Michael Sharman | last post by:
Hi guys, I'm a little confused with dates. Ok, all I want to do is store a date in MySQL as a datetime object and be able to read and format it using PHP. My datatype in MySQL is DATETIME and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.