VB code for best way for check Date is less then or Grater then
What is the best way for date > or <
Dim mMyDate as date Then we store date from perticuler tabel using Record Set like mMyDate=MyRst!Date_Of_Joining If mMyDate > now then Msgbox " Date_Of_Joining > Today" it is ok or we need to use format functione like If format(mMyDatem,"DD/MM/YYYY") > format(now,"DD/MM/YYYY") then Msgbox " Date_Of_Joining > Today" End if and one thing if we will use format(mMyDatem,"MM/DD/YYYY") > format(now,"MM/DD/YYYY") then result will be same or not?
Answer (2)
The only correct way to test for formatted dates being greater or less than another is to format them in yyyyMMdd format.
_
For example, using the below with myDatem being 05/07/2009 and today being 04/07/2010, you would get 05/07/2009 being later than today which is clearly not the case. You must ensure the date values are left padded with zeroes for the months of January thru September and the day of the month < 10 and the way do to this is the use the yyyyMMdd format characters. _ If you were to format them as 2009/07/05 compared to 2010/07/04, the comparison would work correctly. _ Hope this helps.
Answer (2)
If you have dates in the form of Date values (for example, if you retrieve the value from a Date/Time field to a Date variable, or if you get the value of the Now() function), you should compare them AS DATES: * Comparison as dates if much faster than comparison as strings * Conversion to formatted strings adds even more unnecessary processing time * String conversions can lead to incorrect results In particular, the following statement: .. If Format(myDate, "dd/mm/yyyy") > Format (Now(), "dd/mm/yyyy") .. will give the wrong result approximately HALF THE TIME, because string comparisons will compare the day number BEFORE comparing the month number or year number. Therefore, 29/01/1999 will be greater than 01/12/2010, because 29 is greater 01. There is absolutely NO reason to convert dates to strings to perform comparisons for greater than or less than. However, you do need to be careful when comparing dates for equality. A Date may be for any time of day. The statement: .. myDate = CDate("04/07/2010") .. will create a date/time value that is equal to: .. 2010/07/04 00:00:00 .. but the function Now() will return a value that may have any time value at all. If you plan to do a comparison for equality, make sure that you TRUNCATE your date/time value to only the date. While one way to do that is to convert the date to a formatted string and then convert the string back to a date, it's extremely inefficient. A much more efficient way to do it is to use the various date/time functions: .. Dim today As Date today = Now() today = DateSerial(Year(today ), Month(today ), Day(today)) .. In some environments, the Date() function MAY return a date/time value that contains only the date (and an effective time of midnight). You should verify that before using the Date() function without applying truncation to the results.
Michael S. Meyers-Jouan
|
No comments:
Post a Comment