Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing dates in vb6

Status
Not open for further replies.

moben

IS-IT--Management
Feb 5, 2002
116
0
0
GB
Can anyone tell me the best way to compare 2 dates rather than using the DateDiff function.

Using date1>date2 gives inconsistant results, although I would like to use this type of format

Any help greatly appreciated.

Thanks.
 
How about:

Code:
Format(strDate1, "dd/mm/yyyy") > Format(strDate2, "dd/mm/yyyy")
 
I would take a look at the DateDiff function.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 

>compare 2 dates rather than using the DateDiff function

>Using date1>date2 gives inconsistant results
It shouldn't. Are both variables of type "Date"?

If so, then post an example...
 
I think ca8msm was on the right track.

Dates can be copared directly, so if you arent getting the expected results, you might try typecasting it using CDATE() just to make sure.

Thats one thing ive learned is to typecast it anyways, saves me time coming back and debugging some dumb invalid data type later.
 

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
?&quot;10&quot; < &quot;9&quot;
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....
 
ca8msm is close...so close...

To compare dates as strings, you should format them so that the value that changes least often is on the left (just like numbers). So it should read:
Code:
Format(strDate1, &quot;yyyy-mm-dd&quot;) > Format(strDate2, &quot;yyyy-mm-dd&quot;)
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.

Chip H.


 
>you should compare date values as dates, which means using DateDiff
Sure it's possible to use the DateDiff() function.

But, I beleive it's not needed if just comparing dates, when the dates are in, or converted to, Date types.

Dim Date1 As Date
Dim Date2 As Date

Date1 = #somedate# 'Or = CDate(SomeDateString)
Date2 = #somedate#'Or = CDate(SomeDateString)

If = Date1 <> Date2 Then

Which is much much of faster than the DateDiff().

Or

Dim Date1 As String
Dim Date2 As String

Date1 = SomeDateString
Date2 = SomeDateString

If = CDate(Date1) <> CDate(Date2) Then

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:

date1>date2

and not to get the actual difference.



 
CCLINT -
You are correct - if all you care is that one date occurs before another one, then just using the less-than operator is all you need to do.

I should read the questions more carefully...

Thanks.
Chip H.
 
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(&quot;dd&quot;,-1,Now)... dateadd(&quot;dd&quot;,-7&quot;,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, &quot;yyyy-mm-dd&quot;)

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.

************
RudeJohn
************
 
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.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top