2013-04-30 56 views
0

我搜索,但无法找到我在找什么。如何在ASP Classic中将Date()转换为dd-monthname-YYYY?

如何将ASP Classic中的正常Date()转换为dd-monthname-YYYY格式的字符串?

下面是一个例子:

Old date (mm/dd/YYYY) : 5/7/2013 
New date (dd-monthname-YYYY) : 7-May-2013 
+0

注:您的格式说明不匹配你的例子。例如,'mm/dd/YYYY'将是'05/07/2013'。您显示的日期格式为“m/d/yyyy”。 – ErikE 2013-04-30 17:59:44

回答

3
Dim Dt 
Dt = CDate("5/7/2013") 
Response.Write Day(Dt) & "-" & MonthName(Month(Dt)) & "-" & Year(Dt) 
' yields 7-May-2013 

' or if you actually want dd-monthname-YYYY instead of d-monthname-YYYY 
Function PadLeft(Value, Digits) 
    PadLeft = CStr(Value) 
    If Len(PadLeft) < Digits Then 
     PadLeft = Right(String(Digits, "0") & PadLeft, Digits) 
    End If 
End Function 

Response.Write PadLeft(Day(Dt), 2) & "-" & MonthName(Month(Dt)) & "-" & Year(Dt) 
'yields 07-May-2013 

我写了一个ASP经典的日期处理对象而回,可能是你有帮助。它有一个.Format()方法,可以让你传入格式说明符,就像VB/VBA中的Format()函数一样。如果缺少任何部分,我表示歉意 - 但这应该是自然日期格式化方面的巨大飞跃。

Private pMillisecondMatch 
Function RemoveMillisecondsFromDateString(DateString) ' Handle string dates from SQL Server that have milliseconds attached 
    If IsEmpty(pMillisecondMatch) Then 
     Set pMillisecondMatch = New RegExp 
     pMillisecondMatch.Pattern = "\.\d\d\d$" 
     pMillisecondMatch.Global = False 
    End If 
    RemoveMillisecondsFromDateString = pMillisecondMatch.Replace(DateString, "") 
End Function 

Function DateConvert(DateValue, ValueIfError) 
    On Error Resume Next 
    If IsDate(DateValue) Then 
     DateConvert = CDate(DateValue) 
     Exit Function 
    ElseIf TypeName(DateValue) = "String" Then 
     DateValue = RemoveMillisecondsFromDateString(DateValue) 
     If IsDate(DateValue) Then 
     DateConvert = CDate(DateValue) 
     Exit Function 
     End If 
    End If 
    DateConvert = ValueIfError 
End Function 

Class AspDate 
    Private pValue 

    Public Default Property Get Value() 
     Value = pValue 
    End Property 

    Public Property Set Value(DateValue) 
     If TypeName(DateValue) = "AspDate" Then 
     pValue = DateValue.Value 
     Else 
     Err.Raise 60020, "Class AspDate: Invalid object type " & TypeName(DateValue) & " passed to Value property." 
     End If 
    End Property 

    Public Property Let Value(DateValue) 
     pValue = DateConvert(DateValue, Empty) 
    End Property 

    Public Property Get FormattedDate() 
     FormattedDate = Format("yyyy-mm-dd hh:nn:ss") 
    End Property 

    Public Function Format(Specifier) 
     Dim Char, Code, Pos, MonthFlag 
     Format = "": Code = "" 
     If IsEmpty(Value) Then 
     Format = "(Empty)" 
     End If 
     Pos = 0 
     MonthFlag = False 
     For Pos = 1 To Len(Specifier) + 1 
     Char = Mid(Specifier, Pos, 1) 
     If Char = Left(Code, 1) Or Code = "" Then 
      Code = Code & Char 
     Else 
      Format = Format & Part(Code, MonthFlag) 
      Code = Char 
     End If 
     Next 
    End Function 

    Private Function Part(Interval, MonthFlag) 
     Select Case LCase(Left(Interval, 1)) 
     Case "y" 
      Select Case Len(Interval) 
       Case 1, 2 
        Part = Right(CStr(Year(Value)), 2) 
       Case 3, 4 
        Part = Right(CStr(Year(Value)), 4) 
       Case Else 
        Part = Right(CStr(Year(Value)), 4) 
      End Select 
     Case "m" 
      If Not MonthFlag Then ' this is a month calculation 
       MonthFlag = True 
       Select Case Len(Interval) 
        Case 1 
        Part = CStr(Month(Value)) 
        Case 2 
        Part = Right("0" & CStr(Month(Value)), 2) 
        Case 3 
        Part = MonthName(Month(Value), True) 
        Case 4 
        Part = MonthName(Month(Value)) 
        Case Else 
        Part = MonthName(Month(Value)) 
       End Select 
      Else ' otherwise it's a minute calculation 
       Part = Right("0" & Minute(Value), 2) 
      End If 
     Case "n" 
      Part = Right("0" & Minute(Value), 2) 
     Case "d" 
      Part = CStr(Day(Value)) 
      If Len(Part) < Len(Interval) Then 
       Part = Right("0" & Part, Len(Interval)) 
      End If 
     Case "h" 
      MonthFlag = True 
      Part = CStr(Hour(Value)) 
      If Len(Part) < Len(Interval) Then 
       Part = Right("0" & Part, Len(Interval)) 
      End If 
     Case "s" 
      Part = Right("0" & Second(Value), 2) 
     Case Else ' The item is not a recognized date interval, just return the value 
      Part = Interval 
     End Select 
    End Function 
End Class 

Function NewDate(Value) 
    Set NewDate = New AspDate 
    NewDate.Value = Value 
End Function 

Function NewDateWithDefault(Value, DefaultValue) 
    Set NewDateWithDefault = New AspDate 
    If Value = Empty Then 
     NewDateWithDefault.Value = DefaultValue 
    Else 
     NewDateWithDefault.Value = Value 
    End If 
End Function 

下面是一个使用上面的类实例代码:

<%=NewDate(Checkin.Parameters.Item("@DOB").Value).Format("mm/dd/yyyy")%> 

为了让您在上面提到的格式,你会怎么做:

.Format("d-mmmm-yyyy")