why is "8/3/2004" > "8/10/2004"?

B

Bjorn

Hi,

I need to determine the hightest date between e.g. "8/3/2004" and
"8/4/2004".
With this code, i get 'dat2' , which means that "8/4/2004" is higher than
"8/3/2004". That 's right.
But with "8/3/2004" and from "8/10/2004" (also "8/11/2004" ...) i get
'dat1'.
<%
dat1="8/3/2004"
dat2="8/10/2004"
if dat1>dat2 then
response.write("dat1")
else
response.write("dat2")
end if
%>

Why does that not produce 'dat2'?
Thanks
bjorn
 
B

Bob Barrows [MVP]

Bjorn said:
Hi,

I need to determine the hightest date between e.g. "8/3/2004" and
"8/4/2004".
With this code, i get 'dat2' , which means that "8/4/2004" is higher
than "8/3/2004". That 's right.
But with "8/3/2004" and from "8/10/2004" (also "8/11/2004" ...) i get
'dat1'.
<%
dat1="8/3/2004"
dat2="8/10/2004"
if dat1>dat2 then
response.write("dat1")
else
response.write("dat2")
end if
%>

Why does that not produce 'dat2'?

Because you are comparing strings. "8/3" is greater than "8/1" when you are
ordering them "alphabetically". You need to convert the strings to dates in
order to compare them as dates. Use the CDate() function, or better yet,
Dateserial(). If you do not have the vbscript documentation, you can
download it from here:
http://tinyurl.com/7rk6

HTH,
Bob Barrows
 
C

Christopher Brandsdal

<%

dat1=FormatDateTime(#8/3/2004 11:59 AM#, 2)

dat2=FormatDateTime(#8/10/2004 11:59 AM#, 2)

if dat1>dat2 then

response.write("dat1")

else

response.write("dat2")

end if

%>
 
C

CJM

The other posters seem to be pointing you in the right direction, but I
thought I'd mention that it is much better if you use a universal date
format since your '8/3/2004' could refer to 8th March or 3rd August
depending on other factors. '20040803' is [unambiguously] March 3rd.

See http://www.aspfaq.com/show.asp?id=2023
 
D

David C. Holley

And actually, I'm willing to bet that that's exactly what's going.
You're comparing 08 MAR 2004 to 03 AUG 2004.

David H
 
A

Aaron [SQL Server MVP]

No, it wouldn't take one m/d/y and another m/d/y and only transpose one of
them.

The problem is that the string "8/3..." comes before the string "8/1..."

--
http://www.aspfaq.com/
(Reverse address to reply.)




David C. Holley said:
And actually, I'm willing to bet that that's exactly what's going.
You're comparing 08 MAR 2004 to 03 AUG 2004.

David H
The other posters seem to be pointing you in the right direction, but I
thought I'd mention that it is much better if you use a universal date
format since your '8/3/2004' could refer to 8th March or 3rd August
depending on other factors. '20040803' is [unambiguously] March 3rd.

See http://www.aspfaq.com/show.asp?id=2023
 
B

Bob Barrows [MVP]

Aaron said:
No, it wouldn't take one m/d/y and another m/d/y and only transpose
one of them.

The problem is that the string "8/3..." comes before the string
"8/1..."
Hmm - that statement sounds familiar ... ;-)
 
D

Dave Anderson

David said:
And actually, I'm willing to bet that that's exactly what's going.
You're comparing 08 MAR 2004 to 03 AUG 2004.

More to the point, CYMD formats can be compared as strings, so no date
conversion is required for sorting. Together with its complete lack of
ambiguity, this format is hard to beat.



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top