首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBscript中的FormatDateTime出错

VBscript中的FormatDateTime出错
EN

Stack Overflow用户
提问于 2013-05-13 07:24:19
回答 1查看 4.2K关注 0票数 0

我是一个运行的VB脚本与asp经典,我得到了以下错误:

代码语言:javascript
复制
Microsoft VBScript runtime error '800a0005'

Invalid procedure call or argument: 'FormatDateTime'

/whatsnew/updated_pages_www.htm, line 52

我正在试图找出是什么导致了这个错误。csv文件中的日期格式是否有问题?日期格式为: 20090220122443

页面代码如下:

代码语言:javascript
复制
<%@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"-->
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-13 08:46:02

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

根据您所在位置使用的日期/时间格式,可能如下所示(例如:美国日期格式):

代码语言:javascript
复制
"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"

作为测试,您可以检查日期的有效性,如下所示:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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"是一个有效的日期/时间,但它的格式无效。

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

代码语言:javascript
复制
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接受您格式的日期/时间字符串,并返回FormatDateTime函数也可以接受的date (CDate)。

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

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

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

代码语言:javascript
复制
createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)

编辑:

下面是关于如何将该函数添加到.asp页面以及如何使用该函数的更详细的解释。

首先,需要将函数添加到ASP页面。

编辑您的页面。

通常,函数声明放在<head>部分中,如下所示:

代码语言:javascript
复制
<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>部分中,如下所示:

代码语言:javascript
复制
<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>标记的部分可能包含在所包含的文件中。您可以通过检查文件来验证这一点:

代码语言:javascript
复制
"/_lib/include/header.htm"
"/_lib/include/menu.htm"

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

代码语言:javascript
复制
<!--#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
%>

或者,您可以将函数声明插入到这些文件的one中:

代码语言:javascript
复制
"/_lib/include/header.htm"
"/_lib/include/menu.htm"

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

这将是(最有可能的)所有这些:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
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)

因此,如果您在问题中提供的代码仍然正确和完整,下面是使用日期转换函数的相同代码:

代码语言:javascript
复制
<%@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文件的副本,这将非常有帮助

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16513036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档