Active Server Pages Programming (ASP)/Convert string into date...using ASP

Advertisement


Question
QUESTION: 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.

Answer
Hi,

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  

Active Server Pages Programming (ASP)

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ashley Brazier

Expertise

Main focus is ASP, SQL Server 2000 & 2005

Experience

Over 6 years experience.

Education/Credentials
BCs Honours Software Design and Networks

©2012 About.com, a part of The New York Times Company. All rights reserved.