|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dates at Midnightfollows: 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? Brian8***@gmail.com wrote:
Show quoteHide quote > I'm having a problem with VB 2005. I'm trying to compare 2 dates as How are the AddTo and AddFrom variables populated? Do they come from a> 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? 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 <Brian8***@gmail.com> wrote in message
Show quoteHide quote news:1151374604.851660.309010@i40g2000cwc.googlegroups.com... Is this the result you're seeing or the result you're expecting? Using ISO > 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 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 wrongThis 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 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) :-) CorShow 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 > 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 -- Show quoteHide quoteHope 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 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? | 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.
Controling Event Sequencing...
dataset and where clause Dataset HasChanges function not true when Dataset is passed as variable button flash with red and white color How to detect datagrid row change Default value not being populated in dataset Insert text into textbox at caret? [Regular Expression] match a word with interpunctuation Not 'breaking' or executing statments. Getting saved values back to display after Update |
|||||||||||||||||||||||