sephiith
sephiith

Reputation: 1197

Error with FormatDateTime in VBscript

I am a running VB script with asp classic and I am getting the following error:

Microsoft VBScript runtime error '800a0005'

Invalid procedure call or argument: 'FormatDateTime'

/whatsnew/updated_pages_www.htm, line 52

I am trying to work out what is causing the error. Is there anything wrong with the format of the date in the csv file? The date format is: 20090220122443

PAGE CODE BELOW:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Response.CharSet = "UTF-8" %>
<%
pagetitle="What was published last week on casa.gov.au"
%>

<%
connectString = "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & Server.MapPath("/whatsnew/data")

set connect = Server.CreateObject("ADODB.connection")
connect.open connectString
selectSQL = "SELECT * FROM www.csv" 

set www_RS = connect.execute(selectSQL)

%>



<!--#INCLUDE VIRTUAL="/_lib/include/header.htm"-->
<!--#INCLUDE VIRTUAL="/_lib/include/menu.htm"-->
<p class="breadCrm"><a href="/index.htm">Home</a>  <span>&gt;</span> <a href="/whatsnew/index.htm">What's New</a></p>
<!--<img src="/_lib/images/menu/yourarea.gif" alt="" width="891" /> -->
<div class="twoColumnRow">
<div class="twoColumnContent">
<div class="contentPad">
<!-- Start of main content -->

<p class="imageRight">&nbsp;</p>
<h1><%=pagetitle%></h1>



<%
www_RS.MoveFirst

%>


  <caption><h3>New or Amended on www.casa.gov.au</h3></caption>
<ul class="relatedInfoLinks">
<%
Dim pagecode, pagecode2, newfiledate, publisheddate, moddate, createddate, newfilediff, recently_published
pagecode = www_RS("PAGECODE").Value

While not www_RS.EOF

pagecode = www_RS("PAGECODE").Value
pagecode2 = Instr(pagecode,"PC_")
pagecode3 = Instr(pagecode,"~")

createddate = FormatDateTime(www_RS("CREATED_DATE").Value,1)
moddate = FormatDateTime(www_RS("MOD_DATE").Value,1)
publisheddate = FormatDateTime(www_RS("PUB_DATE").Value,1)
newfilediff =  DateDiff("y",www_RS("CREATED_DATE").Value,www_RS("PUB_DATE").Value)
recently_published = DateDiff("y",www_RS("PUB_DATE").Value,dtNow)

if (pagecode2 = 1) and (pagecode3 = 0) and (recently_published < 8) then

%> 
<li>
<%
 Response.Write("<a href='http://casa.gov.au/scripts/nc.dll?WCMS:STANDARD::pc=" & pagecode & "'>" & www_RS("DESCRIPTION").Value & "</a>")
%> 

<BR>
Last modified <%=publisheddate%>
<BR>
<%
if newfilediff < 8 then 
%>
<span style="color:red">* This is a new page</span>
<%
end if
%>
</li>
<BR>
<%
end if 

www_RS.MoveNext

Wend

%>
 </ul>


<!-- End of main content -->
</div> <!-- end contentPad div -->
</div> <!-- end twocolumncontent div -->
<div class="twoColumnLinks">

<!--#INCLUDE VIRTUAL="/_lib/include/quicklinks.htm"-->
<!--#INCLUDE VIRTUAL="/_lib/include/mylinks.htm"-->
</div> <!-- end twocolumnlinks div -->
</div> <!-- end twocolumnrow div -->
<!--#INCLUDE VIRTUAL="/_lib/include/footer.htm"-->

Upvotes: 0

Views: 5103

Answers (1)

Kevin Fegan
Kevin Fegan

Reputation: 1290

The FormatDateTime function requires a validly formatted date as the first parameter.

Depending on the date/time format used in your location, it will be something like this (example: US date format):

"02-20-2009 11:24:43 AM"
"02/20/2009 11:24:43 AM"
"02-20-2009 14:24:43"
"02/20/2009 14:24:43"

As a test, you can check the validity of a date like this:

d = CDate("02-20-2009 11:24:43 AM")
d = CDate("02/20/2009 11:24:43 AM")
d = CDate("02-20-2009 14:24:43")
d = CDate("02/20/2009 14:24:43")

Or, using IsDate:

b = IsDate("02-20-2009 11:24:43 AM")
b = IsDate("02/20/2009 11:24:43 AM")
b = IsDate("02-20-2009 14:24:43")
b = IsDate("02/20/2009 14:24:43")

In your case, your date string: "20090220122443" is a valid date/time, but it is not in a valid date/time format.

You could use a function to convert your date string into a valid format. Here is an example of some code to do the conversion.

strDate = "20090220122443"

wscript.echo "strConvertDateString(strDate) =" & strConvertDateString(strDate)
wscript.echo "dateConvertDateString(strDate)=" & dateConvertDateString(strDate)
wscript.echo

wscript.echo FormatDateTime(strConvertDateString(strDate),1)
wscript.echo FormatDateTime(dateConvertDateString(strDate),1)
wscript.echo


Function strConvertDateString (strDateString)
    strConvertDateString = mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2)
End Function

Function dateConvertDateString (strDateString)
    dateConvertDateString = CDate(mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2))
End Function

The function strConvertDateString accepts a date/time string in your format and returns a string in a format acceptable to the FormatDateTime function.

The function dateConvertDateString accepts a date/time string in your format and returns a date (CDate), which is also acceptable to the FormatDateTime function.

The first one should work in most locations. The second might work better if there are issues with date conversions particular to your location. You should only need to implement and use one of these two functions.

In any case, depending on your location, you may need to edit the functions to adjust the way that mid() is used to compose the date/time string.

Note: This was written for testing in VBScript (cscript.exe). Copy/cut/paste the Function into your .asp file for use. Then use the function like this:

createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)



Edit:

Here is a more detailed explaination of how to add this function to your .asp page and how to use the function.

First, you need to add the function to your ASP page.

Edit your page.

Normally, function declarations are placed inside the <head> section like this:

<html>
<head>
...
...
<%
    Function dateConvertDateString (strDateString)
        dateConvertDateString = CDate(mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2))
    End Function
%>
</head>
<body>
...
<!-- there will be more <html> or <% asp code %> here ... -->

Or, inside the <body> section like this:

<html>
<head>
...
...
</head>
<body>
<%
    Function dateConvertDateString (strDateString)
        dateConvertDateString = CDate(mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2))
    End Function
%>
...
<!-- there will be more <html> or <% asp code %> here ... -->

In your case, it looks like the sections of the page that contain the <html>, <head>, </head>, and <body> tags could be contained in the included files. You can verify that by examining the files:

"/_lib/include/header.htm"
"/_lib/include/menu.htm"

So, in your case, you'll want to insert the function declaration into your page after the <!--#INCLUDE VIRTUAL= lines, like:

<!--#INCLUDE VIRTUAL="/_lib/include/header.htm"-->
<!--#INCLUDE VIRTUAL="/_lib/include/menu.htm"-->
<%
    Function dateConvertDateString (strDateString)
        dateConvertDateString = CDate(mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2))
    End Function
%>

Or, you could probably insert the function declaration into one of these files:

"/_lib/include/header.htm"
"/_lib/include/menu.htm"

Next, find the statements that are using VBScript date functions where you are currently passing dates formatted like this: "20090220122443"

That would be (most likely) all of these:

createddate = FormatDateTime(www_RS("CREATED_DATE").Value,1)
moddate = FormatDateTime(www_RS("MOD_DATE").Value,1)
publisheddate = FormatDateTime(www_RS("PUB_DATE").Value,1)
newfilediff =  DateDiff("y",www_RS("CREATED_DATE").Value,www_RS("PUB_DATE").Value)
recently_published = DateDiff("y",www_RS("PUB_DATE").Value,dtNow)

and edit those statements to use the function to convert the dates, like this:

createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)
createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)
moddate = FormatDateTime(dateConvertDateString(www_RS("MOD_DATE").Value),1)
publisheddate = FormatDateTime(dateConvertDateString(www_RS("PUB_DATE").Value),1)
newfilediff =  DateDiff("y",dateConvertDateString(www_RS("CREATED_DATE").Value),dateConvertDateString(www_RS("PUB_DATE").Value))
recently_published = DateDiff("y",dateConvertDateString(www_RS("PUB_DATE").Value),dtNow)

So, if the code you provided in your question is still correct and complete, here is the same code using the date conversion function:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Response.CharSet = "UTF-8" %>
<%
pagetitle="What was published last week on casa.gov.au"
%>

<%
connectString = "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & Server.MapPath("/whatsnew/data")

set connect = Server.CreateObject("ADODB.connection")
connect.open connectString
selectSQL = "SELECT * FROM www.csv" 

set www_RS = connect.execute(selectSQL)

%>



<!--#INCLUDE VIRTUAL="/_lib/include/header.htm"-->
<!--#INCLUDE VIRTUAL="/_lib/include/menu.htm"-->
<%
    Function dateConvertDateString (strDateString)
        dateConvertDateString = CDate(mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2))
    End Function
%>

<p class="breadCrm"><a href="/index.htm">Home</a>  <span>&gt;</span> <a href="/whatsnew/index.htm">What's New</a></p>
<!--<img src="/_lib/images/menu/yourarea.gif" alt="" width="891" /> -->
<div class="twoColumnRow">
<div class="twoColumnContent">
<div class="contentPad">
<!-- Start of main content -->

<p class="imageRight">&nbsp;</p>
<h1><%=pagetitle%></h1>



<%
www_RS.MoveFirst

%>


  <caption><h3>New or Amended on www.casa.gov.au</h3></caption>
<ul class="relatedInfoLinks">
<%
Dim pagecode, pagecode2, newfiledate, publisheddate, moddate, createddate, newfilediff, recently_published
pagecode = www_RS("PAGECODE").Value

While not www_RS.EOF

pagecode = www_RS("PAGECODE").Value
pagecode2 = Instr(pagecode,"PC_")
pagecode3 = Instr(pagecode,"~")

createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)
createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)
moddate = FormatDateTime(dateConvertDateString(www_RS("MOD_DATE").Value),1)
publisheddate = FormatDateTime(dateConvertDateString(www_RS("PUB_DATE").Value),1)
newfilediff =  DateDiff("y",dateConvertDateString(www_RS("CREATED_DATE").Value),dateConvertDateString(www_RS("PUB_DATE").Value))
recently_published = DateDiff("y",dateConvertDateString(www_RS("PUB_DATE").Value),dtNow)


if (pagecode2 = 1) and (pagecode3 = 0) and (recently_published < 8) then

%> 
<li>
<%
 Response.Write("<a href='http://casa.gov.au/scripts/nc.dll?WCMS:STANDARD::pc=" & pagecode & "'>" & www_RS("DESCRIPTION").Value & "</a>")
%> 

<BR>
Last modified <%=publisheddate%>
<BR>
<%
if newfilediff < 8 then 
%>
<span style="color:red">* This is a new page</span>
<%
end if
%>
</li>
<BR>
<%
end if 

www_RS.MoveNext

Wend

%>
 </ul>


<!-- End of main content -->
</div> <!-- end contentPad div -->
</div> <!-- end twocolumncontent div -->
<div class="twoColumnLinks">

<!--#INCLUDE VIRTUAL="/_lib/include/quicklinks.htm"-->
<!--#INCLUDE VIRTUAL="/_lib/include/mylinks.htm"-->
</div> <!-- end twocolumnlinks div -->
</div> <!-- end twocolumnrow div -->
<!--#INCLUDE VIRTUAL="/_lib/include/footer.htm"-->


To give this a try, copy your current .htm file to something like whatever.htm. Then, replace (or edit) your current .htm with the code above.

Unfortunately, I don't have a way to test your complete ASP page.

If you still have problems with this, it would be very helpful if you could provide a copy of your actual .htm file, and a copy of the files: /_lib/include/header.htm, and /_lib/include/menu.htm

Upvotes: 5

Related Questions