我需要在vbscript中将日期从mm / dd / yyyy转换为dd / mm / yyyy

时间:2010-03-24 21:29:37

标签: date vbscript format

到目前为止我有这个: FormatDateTime(negativeItemsRS(“ItemDate”),0)

以mm / dd / yyyy的格式显示日期。我想将其转换为dd / mm / yyyy

请帮助不知道如何做到这一点。

谢谢,

3 个答案:

答案 0 :(得分:1)

您必须将locale id设置为使用所需日期格式的{{3}}。我不记得在哪里使用哪种格式,但英国(2057)或美国(1033)都可以使用。

您尚未指定环境。在ASP中,您可以在Language指令或Session或Response类中使用LCID属性,具体取决于您对设置所需的范围:

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

Session.LCID = 1033

Response.LCID = 1033

答案 1 :(得分:0)

您需要的是FormatDate函数。我以前用手工连接的方式很难做到这一点,但我发现有一些.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类留在那里以供参考,以防任何人感兴趣。 (但我确实交换了两者的顺序,以使顶部显示的代码更适用于问题)。

答案 2 :(得分:0)

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

让我们说&#34; date1&#34; (MM / DD / YY)= 03/06/2014 我想&#34; date2&#34;以DD / MM / YY为06/03/2014

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

将提供所需的结果。