2010-03-24 52 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 

能不能給這要求的結果。