Active Server Pages Programming (ASP)/Convert string into date...using ASP
Expert: Ashley Brazier - 2/12/2009
QuestionQUESTION: Hi Ashley, I am having trouble converting a string in the format of YYYY-MM-DD into MM/DD/YYYY. I tried using the cdate function, which works well, except that I have some null records in that field. It breaks right when a date record has a null.
ANSWER: Hi,
Have you tried
if yourdate = "" then
Or
if isnull(yourdate) then
Can you send sample code?
---------- FOLLOW-UP ----------
QUESTION: strNewDate = (rsdept("fhnpro"))
response.write"<td align=center><font>" & strNewDate & "</font></td>" & _
The output appears as: 2009-02-20
I need it to appear as: 02/20/2009
This code below will work and format the date correctly, but will break when a null appears.
"<td align=center><font>" & cdate(strNewDate) & "</font></td>" & _
From what I have read, the cdate function doesn't work with null values
This is the error that I get with the cdate function when a null value appears:
Microsoft VBScript runtime error '800a005e'
Invalid use of Null: 'cdate'
Yes, I have tried the isnull if statement and could not get it to work. Although, i wouldn't doubt if it was a syntax issue.
AnswerHi,
Your best bet is to check if its null before trying to display it.
Is the error coming up on this line:
"<td align=center><font>" & cdate(strNewDate) & "</font></td>" & _
if it does, what is the value in the database, is it NULL or the default 01/01/1900?
try this, put this after the strNewDate = (rsdept("fhnpro"))
if isnull(strNewDate) OR strNewDate <> "" then
strNewDate = cdate(strNewDate)
else
strNewDate = "NULL DATE HERE"
end if
Change this line:-
"<td align=center><font>" & cdate(strNewDate) & "</font></td>" & _
to
"<td align=center><font>" & strNewDate & "</font></td>" & _
Let us know how you get on
Cheers