Basically said, as I tried had asked, if both variables are DATE variables, (Or are converted to dates) then a comparison between the variables will always work correctly - you are actually then comparing two numbers
If not, then you are comparing two strings - and that is a totally different type of comparison, just like
?"10" < "9"
will return True, but
?10 < 9
will return False, because now you are actually using numbers.
Same thing with Dates. They need really be dates, and not strings, in order to compare correctly....
This, BTW, is ISO-8601 format, an international standard.
But really, CCLINT is even more correct - you should compare date values as dates, which means using DateDiff. Recall that objects (in the broad sense of the meaning) contain data, and the operations which can be performed on that data. The DateDiff function is as close as VB6 can come to this object-oriented ideal.
Both of these examples will compare the dates, AND are much more efficient than using the Format$() or DateDiff() functions. Especially in a long loop you will see the difference in speed.
When comparing something other than the Dates/Days themselves, such as years, then using the Year(), Month() functions are still more efficient than the Format or DateDiff functions.
IF one date variable contains a Date (actually midnight of a certain date), and the other DateTime (other than midnight), then the comparison will not be equal unless you use the DateValue() function on the one containtin the DateTime:
Dim Date1 As String
Dim Date2 As String
Date1 = SomeDateString
Date2 = SomeDateString
If DateValue(date1) = DateValue(date2) Then
This is still faster than the DateDiff()
The primary purpose of the DateDiff() function is not to do a boolean comparison, but to return the actual Difference between two dates.
The original question was to compare:
I have a similar problem to this one. I am trying to do the following:
I have a SELECT object with various OPTIONS. I am generating these OPTIONS based on the NOW function, and the dateadd("dd",-1,Now)... dateadd("dd",-7",Now)
This way the options are today all the way back to a week ago. The days are in the format mm/dd/yyyy
The PROBLEM is that I need to compare these dates with something in a database. The database fields are in ISO format (yyyy-mm-dd). So I need to FORMAT my date field.
I use the following line of code:
startDate = FORMAT(Now, "yyyy-mm-dd"
and I get a type mismatch. StartDate is variant (declared right above) and we all know that Now is a date variable. I tried typecasting Now using CDate and CStr but I always got a type mismatch. Any help would be greatly appreciated.
Date variables (like what comes out of the ADO recordset, and VB's native Date datatype) don't have *any* formatting. They just hold the value.
Formatting only comes into play when you're displaying the date value for the user, or accepting input from the user.
In those cases, you'll want to get the value into a Date variable as quick as you can - having dates stored in string variables is a fast way to get gray hair.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.