0

我是一个正在运行的带有 asp classic 的 VB 脚本,我收到以下错误:

Microsoft VBScript runtime error '800a0005'

Invalid procedure call or argument: 'FormatDateTime'

/whatsnew/updated_pages_www.htm, line 52

我正在尝试找出导致错误的原因。csv文件中日期的格式有什么问题吗?日期格式为:20090220122443

页面代码如下:

<%@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"-->
4

1 回答 1

6

FormatDateTime函数需要一个有效格式化的日期作为第一个参数。

根据您所在位置使用的日期/时间格式,它将是这样的(例如:美国日期格式):

"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"

作为测试,您可以像这样检查日期的有效性:

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")

或者,使用 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")

在您的情况下,您的日期字符串:"20090220122443"是有效的日期/时间,但不是有效的日期/时间格式。

您可以使用函数将日期字符串转换为有效格式。这是一些进行转换的代码示例。

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

该函数strConvertDateString接受您的格式的日期/时间字符串,并以该函数可接受的格式返回一个字符串FormatDateTime

该函数dateConvertDateString接受您格式的日期/时间字符串并返回一个日期(CDate),该日期也是该FormatDateTime函数可接受的。

第一个应该适用于大多数地方。如果您所在位置的日期转换存在问题,则第二种方法可能会更好。您应该只需要实现和使用这两个功能之一。

在任何情况下,根据您所在的位置,您可能需要编辑函数以调整mid()用于构成日期/时间字符串的方式。

注意:这是为在 VBScript (cscript.exe) 中进行测试而编写的。将函数复制/剪切/粘贴到您的.asp文件中以供使用。然后像这样使用函数:

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



编辑:

这里更详细地说明了如何将此功能添加到您的.asp页面以及如何使用该功能。

首先,您需要将该功能添加到您的ASP页面。

编辑您的页面。

通常,函数声明放置在这样的<head>部分内:

<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 ... -->

或者,在这样的<body>部分内:

<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 ... -->

在您的情况下,看起来包含<html><head></head><body>标签的页面部分可能包含在包含的文件中。您可以通过检查文件来验证:

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

因此,在您的情况下,您需要将函数声明插入到页面之后的<!--#INCLUDE VIRTUAL=行中,例如:

<!--#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
%>

或者,您可以将函数声明插入以下文件之一

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

接下来,找到使用 VBScript 日期函数的语句,您当前在其中传递日期格式如下:"20090220122443"

那将是(很可能)所有这些:

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)

并编辑这些语句以使用该函数来转换日期,如下所示:

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)

因此,如果您在问题中提供的代码仍然正确且完整,那么这里是使用日期转换功能的相同代码:

<%@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"-->


要尝试一下,请将您当前的.htm文件复制到类似whatever.htm. 然后,用上面的代码替换(或编辑)您的当前.htm代码。

不幸的是,我没有办法测试您的完整ASP页面。

如果您对此仍有疑问,如果您可以提供实际.htm文件的副本以及文件的副本:/_lib/include/header.htm/_lib/include/menu.htm

于 2013-05-13T00:46:02.440 回答