Wrox Programmer Forums
|
ASP Pro Code Clinic As of Oct 5, 2005, this forum is now locked. No posts have been deleted. Please use "Classic ASP Professional" at: http://p2p.wrox.com/forum.asp?FORUM_ID=56 for discussions similar to the old ASP Pro Code Clinic or one of the other many remaining ASP and ASP.NET forums here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Pro Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 29th, 2003, 03:54 PM
Registered User
 
Join Date: Oct 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Date variable problem

I am having a very stupid problem. Below is the snippet that i have been testing with. My problem is that when the "expiredate" is defined as a variable then inserted into the if statment it doesnt read its value. It always says that it has not expired. Example:

thedate = date
expiredate = "08/03/2003"
if thedate < expiredate then
response.write "Not Expired"
else
response.write "You have Expired"
end if

''this would return "Not Expired"

Now if I remove the variable and directly insert the date, it works! Example:

thedate = date
if thedate < "08/03/2003" then
response.write "Not Expired"
else
response.write "You have Expired"
end if

''this would return "You Have Expired"

I have to use a variable with the form i am creating. Does anyone know why this happens, or if there would be a work around? Thanks



 
Old October 29th, 2003, 05:17 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hmmm, this *is* a stupid problem. I think what happens is that ASP under the hood creates a string type for the expiredate variable, but that it sees it as a "real" date when you compare it directly with another date. It has always been unclear to me how ASP deals with variants that have some underlying value.

Anyway, you can fix it by "casting" the expiredate to a date, using the DateValue method. This will work:
Code:
thedate = Date()
expiredate = DateValue("08/03/2003")
If thedate < expiredate Then
    Response.Write("Not Expired")
Else
    Response.Write("You have Expired")
End If
HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 30th, 2003, 03:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
Default

VBScript doesn't have any types except a Variant, which in turn can have subtypes. If you do myDate = "01/01/2003" then the subtype will probaby be "text", and then when you compare it to a subtype "date", one or the other needs to cast to the other type. If your date subtype is cast to a string, and compared to your initial string, then you may get results other than what you expect.

To be safe, cast the variant of subtype string to subtype date using CDate()

Cheers
Ken

Microsoft MVP - Windows Server (IIS)
www.adOpenStatic.com
 
Old October 30th, 2003, 11:53 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 171
Thanks: 0
Thanked 1 Time in 1 Post
Default

Here you go.

<%
thedate = date()
expiredate = "08/03/2003"
if cDate(thedate) < cDate(expiredate) then
response.write "Not Expired"
else
response.write "You have Expired"
end if
%>






Similar Threads
Thread Thread Starter Forum Replies Last Post
Using month & year variable to get date in range rsearing ASP.NET 2.0 Professional 2 July 5th, 2008 08:30 AM
problem with session variable prabhatsriva01 ASP.NET 2.0 Professional 3 May 10th, 2007 11:41 AM
Session Variable Problem caterpillar General .NET 0 August 17th, 2006 11:26 AM
Sending Date variable into Oracle Database Judex Java Databases 1 September 20th, 2003 05:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.