2010-03-24 53 views

回答

0

你需要的是一个FormatDate函数。我曾经这样用手工concatenation很难做到这一点,但是我发现有几个.NET库可以从COM访问,因此也可以从ASP Classic访问。我的版本利用了这样的事实:我有一个StringBuilder类,它是.NET中的StringBuilder类的一个包装。

'****************************************************************************** 
Public Function FormatDate(sFormat, dDateValue) 
'PURPOSE: To format a date with any arbitrary format 
'ARGS: 
' sFormat is the defined formats as used by the .NET Framework's System.DateTimeFormatInfo. 
'  Note that this format is case-sensitive. 
'CALLS: 
' 1. System.Text.StringBuilder class in the .NET Framework. 
'EXAMPLE CALL: 
' Dim sFormatedDate 
' sFormatedDate = FormatDate("MM/dd/yy", "1/1/1900 12:00 AM") 
' Or 
' sFormatedDate = FormatDate("MM/dd/yyyy", "1/1/1900 12:00 AM") 
'DESIGN NOTE: 
' This function leverages the fact that System.Text.StringBuilder is COMVisible. 
' Thus, we can call its AppendFormat function from here. 
' You can find more information about the format string parameters allowed at 
' http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx 

    Dim oStringBuilder 
    Dim sSbFormatString 
    Dim dDate 

    If Not IsDate(dDateValue) Then 
     FormatDate = vbNullString 
     Exit Function 
    End If 

    On Error Resume Next 

    dDate = CDate(dDateValue) 
    If Err.number <> 0 Then 
     FormatDate = vbNullString 
     Exit Function 
    End If 

    'if an empty format string is passed, then simply return 
    'the value using the default shortdate format. 
    If Len(sFormat & vbNullString) = 0 Then 
     sSbFormatString = "{0:d}" 
    Else 
     sSbFormatString = "{0:" & sFormat & "}" 
    End If 

    Set oStringBuilder = CreateObject("System.Text.StringBuilder") 
    Call oStringBuilder.AppendFormat(sSbFormatString, dDate) 
    FormatDate = oStringBuilder.ToString() 
    Set oStringBuilder = Nothing 
End Function 
'************************************************************************** 
' Use this class to concatenate strings in a much more 
' efficient manner than simply concatenating a string 
' (strVariable = strVariable & "your new string") 
Class StringBuilder 
'PURPOSE: this class is designed to allow for more efficient string 
' concatenation. 
'DESIGN NOTES: 
'  Originally, this class built an array and used Join in the ToString 
'  method. However, I later discovered that the System.Text.StringBuilder 
'  class in the .NET Framework is COMVisible. That means we can simply use 
'  it and all of its efficiencies rather than having to deal with 
'  VBScript and its limitations. 
    Private oStringBuilder 

    Private Sub Class_Initialize() 
     Set oStringBuilder = CreateObject("System.Text.StringBuilder") 
    End Sub 

    Private Sub Class_Terminate() 
     Set oStringBuilder = Nothing 
    End Sub 

    Public Sub InitializeCapacity(ByVal capacity) 
     On Error Resume Next 
     Dim iCapacity 
     iCapacity = CInt(capacity) 
     If Err.number <> 0 Then Exit Sub 
     oStringBuilder.Capacity = iCapacity 
    End Sub 

    Public Sub Clear() 
     Call Class_Initialize() 
    End Sub 

    Public Sub Append(ByVal strValue) 
     Call AppendFormat("{0}", strValue) 
    End Sub 

    Public Sub AppendFormat(ByVal strFormatString, ByVal strValue) 
     Call oStringBuilder.AppendFormat(strFormatString, (strValue & vbNullString)) 
    End Sub 

    'Appends the string with a trailing CrLf 
    Public Sub AppendLine(ByVal strValue) 
     Call Append(strValue) 
     Call Append(vbCrLf) 
    End Sub 

    Public Property Get Length() 
     Length = oStringBuilder.Length 
    End Property 
    Public Property Let Length(iLength) 
     On Error Resume Next 
     oStringBuilder.Length = CInt(iLength) 
    End Property 

    'Concatenate the strings by simply joining your array 
    'of strings and adding no separator between elements. 
    Public Function ToString() 
     ToString = oStringBuilder.ToString() 
    End Function 
End Class 

所以,用这个类,你可以这样做:

FormatDate("dd/MM/yyyy", RS("DateField")) 

注意,在传递的字符串是区分大小写的。

编辑我看到,在某些时候,我修改了我的FormatDate函数以消除使用我的VBScript StringBuilder类,而是直接使用.NET类。如果有人感兴趣,我将把VBScript的StringBuilder类留在那里以供参考。 (然而,我交换了两者的顺序,使得出现在顶端的代码更适用于这个问题)。

1

您必须将locale id设置为使用所需日期格式的那个。我不记得在哪里使用哪种格式,但英国(2057)或美国(1033)应该工作。

您尚未指定您的环境。在ASP中,你可以使用LCID属性的语言指令或会话或响应等级,这取决于你想要什么范围的设置:

<%@Language="VBScript" LCID="1033"%> 

Session.LCID = 1033 

Response.LCID = 1033 
+0

感谢您的帮助人。 – newbie2009 2010-03-24 21:55:41

0

要在VB脚本中将日期从MM/DD/YYY更改为DD/MM/YYYY,可以使用如下所示的非常简单的步骤:

让说: “日期1”(MM/DD/YY)= 2014年3月6日 我想 “日期2” 是在DD/MM/YY为2014年6月3日

d = Day(date1) 
m = Month(date1) 
y = Year(date1) 
date2 = d & "/" & m & "/" & y 

能不能给这要求的结果。