'================================================
'函数名:FormatDate
'作 用:格式化日期
'参 数:DateAndTime ----原日期和时间
' para ----日期格式
' 用法:再ASP中:=FormatDate(rs("pubdate"),6) ,前面的6表示选择要用下面的哪种格式
'返回值:格式化后的日期
'================================================
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
yy = CStr(Year(DateAndTime))
y = Mid(CStr(Year(DateAndTime)),3)
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 = yy & m & d & h & mi & s
'返回12位 直到秒 的时间字符串
Case "3"
strDateTime = yy & m & d & h & mi
'返回12位 直到分 的时间字符串
Case "4"
strDateTime = yy & "年" & 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 "11"
strDateTime = y & "-" & m & "-" & d
Case "12"
strDateTime = y & "/" & m & "/" & d
'下面是年四位数格式
Case "13"
strDateTime = yy & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
Case "14"
strDateTime = yy & "-" & m & "-" & d
Case Else
strDateTime = DateAndTime
End Select
FormatDate = strDateTime
End Function
转载于:https://www.cnblogs.com/leevane/archive/2008/09/26/1299768.html