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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting date from DB to date subtype

Status
Not open for further replies.

vcherubini

Programmer
May 29, 2000
527
US
Hello:

I've got a value in a database stored as a string but its in the form of a date like 7/08/02. It has to be stored as a string because its in a configuration table and the other values can be strings as well, so its just easier to make it a date.

I load the value from the database and convert it into a date subtype with the Cdate function. That works, I think. However, when I try to compare the dates, I get an error. Does ASP not have the ability to compare dates? I would assume so since you can perform mathematical operations on them, or am I just doing something wrong?

[tt]
db_date = db_connection_obj("config_value") ' this gets the date
db_date = Cdate(db_date)

if (db_date < date) then ' i get the error here
response.write(&quot;thing has ended&quot;)
else
response.write(&quot;thing is still going&quot;)
end
[/tt]

Thanks in advance for any help provided.

-Vic vic cherubini
krs-one@cnunited.com
 
use CDate(&quot;variableName&quot;) to convert a database date value for use in ASP.
 
It could aslo be that you have a different date format on the web server, than on the database server. (Even if they run on the same machine, it could still be a wrong configuration!)

Like dd/mm/yy and mm/dd/yy.

Try checking out both dates with something like this:
Code:
Response.write &quot;Month: &quot; & Month(db_date) & &quot;<br>&quot;
Response.write &quot;Day: &quot; & Day(db_date) & &quot;<br>&quot;
Response.write &quot;Year: &quot; & Year(db_date)
Make sure you use a date that is valid no matter which date format are being used, like 01/02/03.

I deal with diffrent web servers and different database servers all the time, so I have made a couple of functions to convert the dates automatically. Very handy. :)

Hope this helps,
Palooka
 
Mind to share the function that you have created? I think I'm running into this problem too.
 
Sure. Nothing revoultionary here. :)

Code:
Public Function ConvDate(d)
	d = Replace(d, &quot;'&quot;, &quot;&quot;)
	ConvDate = Day(d) & &quot;.&quot; & Month(d) & &quot;.&quot; & Year(d)
	'ConvDate = Month(d) & &quot;/&quot; & Day(d) & &quot;/&quot; & Year(d)
End Function

Public Function CheckDate(d)
	If not isDate(d) Then 
		CheckDate = &quot;&quot;
	Else
		Dato = Day(d) & &quot;.&quot; & Month(d) & &quot;.&quot; & Year(d)
		'Dato = Month(d) & &quot;/&quot; & Day(d) & &quot;/&quot; & Year(d)
		CheckDate = &quot;'&quot; & Dato & &quot;'&quot;
	End If
End Function

Palooka
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top