Date Comparison

1. Senior Member
Join Date
Dec 1969
Posts
716

## Date Comparison

When I use the following code, it returns false and I don&#039;t understand why. This is just a simple example. What I really want to know is how to efficently compare dates using the equal test, greater than or equal test, and so on.<BR><BR>zFirstDate = #7/04/01#<BR>zFirstDate = FormatDateTime(zFirstDate,2)<BR><BR>zLastDate = #7/04/01#<BR>zLastDate = FormatDateTime(zLastDate,2)<BR><BR>if zFirstDate = zLastDate then<BR> Response.Write "TRUE"<BR>end if<BR><BR><BR>thanks for your help.<BR><BR>Justin<BR>

2. Senior Member
Join Date
Dec 1969
Posts
716

## Here is what I meant

When I use the following code, it returns false and I don&#039;t understand why. This is just a simple example. What I really want to know is how to efficently compare dates using the equal test, greater than or equal test, and so on.<BR><BR>zFirstDate = #7/04/01#<BR>zFirstDate = FormatDateTime(zFirstDate,2)<BR><BR>zLastDate = #7/04/01#<BR>zLastDate = FormatDateTime(zLastDate,2)<BR><BR>if zFirstDate &#060;= zLastDate then<BR>Response.Write "TRUE"<BR>end if<BR><BR><BR>thanks for your help.<BR><BR>Justin<BR>

3. Senior Member
Join Date
Dec 1969
Posts
716

## Here is what I meant

When I use the following code, it returns false and I don&#039;t understand why. This is just a simple example. What I really want to know is how to efficently compare dates using the equal test, greater than or equal test, and so on.<BR><BR>zFirstDate = #7/04/01#<BR>zFirstDate = FormatDateTime(zFirstDate,2)<BR><BR>zLastDate = #7/31/01#<BR>zLastDate = FormatDateTime(zLastDate,2)<BR><BR>if zFirstDate &#060;= zLastDate then<BR>Response.Write "TRUE"<BR>end if<BR><BR><BR>thanks for your help.<BR><BR>Justin<BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## FormatDateTime turns them into STRINGS...

And you do *NOT* want to compare strings!<BR><BR>Compare DATE values. <BR><BR>d1 = CDate(#7/4/01#)<BR>d2 = CDate("march 15, 2002")<BR>d3 = CDate("31 Dec 1999")<BR>d4 = DateSerial( 2003, 4, 30 )<BR>... etc. ...<BR><BR>If d1 &#062; d2 Then ...<BR><BR>If d4 &#060; d3 Then ...<BR><BR>When you compare strings,<BR><BR>If "31 December 1917" &#062; "1 March 2999" Then<BR><BR>comes out as true, because the very first characters don&#039;t match and "3" is &#062; then "1".<BR><BR>Not sure why your example failed, but in any case *never* compare string-ized forms of numbers or dates (or booleans or ...).<BR><BR><BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
716

## Thank You

Bill, Thank you for the explanation.

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## And here is what I meant...

Add the line:<BR><BR>Response.Write zFirstDate & " &#060;= " & zLastDate & "??&#060;P&#062;"<BR><BR>It will print out the line<BR><BR>7/4/2001 &#060;= 7/31/2001??<BR><BR>And clearly, in STRING form, the first two characters in each string ("7/") match. So when the third characters don&#039;t match, the STRING comparison says "3" is &#060; "4" and your test is false.<BR><BR>NEVER expect string-ized forms of data to compare the way internal forms of that same data do.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•