0

我搜索但找不到我要找的东西。

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

这是一个例子:

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

1 回答 1

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 Classic 日期处理对象,它可能对你有用。它有一种.Format()方法可以让您像Format()VB/VBA 中的函数一样传入格式说明符。如果缺少任何部分,我深表歉意——但这应该是朝着自然日期格式迈出的一大步。

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")
于 2013-04-30T18:00:49.777 回答