ASP 时间格式化函数
代码
ASP 时间格式化函数 好处是你经常按日期排列文章的时候~ 9月3号会比9月21号靠前,很不爽,你格式化日期09月03号(1位数补0)就不会出现这个事情了
Public Function FormatDate(DateAndTime, para)
On Error Resume Next
Dim y, m, d, h, mi, s, strDateTime
FormatDate = DateAndTime
If Not IsNumeric(para) Then Exit Function
If Not IsDate(DateAndTime) Then Exit Function
y = CStr(Year(DateAndTime))
m = CStr(Month(DateAndTime))
If Len(m) = 1 Then m = "0" & m
d = CStr(Day(DateAndTime))
If Len(d) = 1 Then d = "0" & d
h = CStr(Hour(DateAndTime))
If Len(h) = 1 Then h = "0" & h
mi = CStr(Minute(DateAndTime))
If Len(mi) = 1 Then mi = "0" & mi
s = CStr(Second(DateAndTime))
If Len(s) = 1 Then s = "0" & s
Select Case para
Case "1":strDateTime = y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
Case "2":strDateTime = y & "-" & m & "-" & d
Case "3":strDateTime = y & "/" & m & "/" & d
Case "4":strDateTime = y & "年" & m & "月" & d & "日"
Case "5":strDateTime = m & "-" & d
Case "6":strDateTime = m & "/" & d
Case "7":strDateTime = m & "月" & d & "日"
Case "8":strDateTime = y & "年" & m & "月"
Case "9":strDateTime = y & "-" & m
Case "10":strDateTime = y & "/" & m
Case Else
strDateTime = DateAndTime
End Select
FormatDate = strDateTime
End Function
调用方法:<%=FormatDate(now(),2)%>
On Error Resume Next
Dim y, m, d, h, mi, s, strDateTime
FormatDate = DateAndTime
If Not IsNumeric(para) Then Exit Function
If Not IsDate(DateAndTime) Then Exit Function
y = CStr(Year(DateAndTime))
m = CStr(Month(DateAndTime))
If Len(m) = 1 Then m = "0" & m
d = CStr(Day(DateAndTime))
If Len(d) = 1 Then d = "0" & d
h = CStr(Hour(DateAndTime))
If Len(h) = 1 Then h = "0" & h
mi = CStr(Minute(DateAndTime))
If Len(mi) = 1 Then mi = "0" & mi
s = CStr(Second(DateAndTime))
If Len(s) = 1 Then s = "0" & s
Select Case para
Case "1":strDateTime = y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
Case "2":strDateTime = y & "-" & m & "-" & d
Case "3":strDateTime = y & "/" & m & "/" & d
Case "4":strDateTime = y & "年" & m & "月" & d & "日"
Case "5":strDateTime = m & "-" & d
Case "6":strDateTime = m & "/" & d
Case "7":strDateTime = m & "月" & d & "日"
Case "8":strDateTime = y & "年" & m & "月"
Case "9":strDateTime = y & "-" & m
Case "10":strDateTime = y & "/" & m
Case Else
strDateTime = DateAndTime
End Select
FormatDate = strDateTime
End Function
调用方法:<%=FormatDate(now(),2)%>