[thelist] ASP Date Manipulation

Steve Cook steve.cook at evitbe.com
Tue Mar 19 09:46:01 CST 2002


Ahah! I think i have a clue!

Are you using a datetime field in the database? If you're using a varchar,
then when you get the date back from the database it will be in string
format and using a > comparison will be meaningless.

I have 3 possible solutions:
	1) Change to a datetime field in the database. You should probably
also do your comparison in the SQL itself:

	SQL = "SELECT * FROM table WHERE inDate > '" & pastDate & "';"

	2) If you need all the entries, but are only printing the date for
those that are > 3 months old, then you should probably be using dateDiff()
for the comparison:

	if (dateDiff("d", pastdate, rs("date_completed")) > 0 then
		Response.Write "This is within the last 3 months"
	end if

	(You might want to check I've got the order of the dates right - it
could be the other way around!)

	3) You *could* use a varchar for the date, but then when you get it
back from the database you'll need to convert it using:
	realDate = Cdate(rs("date_completed"))

	(You can check whether a value is a date format or not using
	if isDate(rs("date_completed")) = True then

.steve


----------------------------------
   WapWarp - http://wapwarp.com
 Wap-Dev - http://www.wap-dev.net
 Cookstour - http://cookstour.org
----------------------------------



> -----Original Message-----
> From: Feingold Josh S [mailto:Josh.S.Feingold at irs.gov]
> Sent: den 19 mars 2002 16:12
> To: 'thelist at lists.evolt.org'
> Subject: [thelist] ASP Date Manipulation
>
>
> This message is in MIME format. Since your mail reader does
> not understand
> this format, some or all of this message may not be legible.
> --
> [ Picked text/plain from multipart/alternative ]
> I am trying to show only those records where the date is not
> older than 3
> months from a date in the database.
>
> Here is my code:
>
> pastdate = DateAdd("m",-3,Now())  'pastdate is now three months ago
>
> do while not rs.eof
>     if rs("date_completed") > pastdate then  'if date in
> database is newer
> than date three months ago
>         response.write pastdate  'show the date
>     end if
> loop
>
> I tried using both 2/01/02 and 2/01/02 in the database and
> neither makes the
> date show.
>
> Anyone know where I am wrong?
>
> Thanks,
> Josh
> --
> For unsubscribe and other options, including
> the Tip Harvester and archive of thelist go to:
> http://lists.evolt.org Workers of the Web, evolt !
>



More information about the thelist mailing list