网络编程
位置:首页>> 网络编程>> Asp编程>> ASP格式化日期的函数(输出13种样式)

ASP格式化日期的函数(输出13种样式)

  发布时间:2011-07-12 20:22:00 

标签:日期,格式,函数

 

'================================================
 '函数名:FormatDate
 '作  用:格式化日期
 '参  数:DateAndTime   ----原日期和时间
 '        para   ----日期格式
 '返回值:格式化后的日期
 '================================================
 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 & " " & h & ":" & mi
  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 = right(y,2) & "-" &m & "-" & d & " " & h & ":" & mi
  Case "12"
   strDateTime = right(y,2) & "-" &m & "-" & d
  Case "13"
   strDateTime = m & "-" & d
  Case Else
   strDateTime = DateAndTime
  End Select
 FormatDate = strDateTime
End Function

 

函数2,6种显示

 

<%
' ============================================
' 格式化时间(显示)
' 参数:n_Flag
' 1:"yyyy-mm-dd hh:mm:ss"
' 2:"yyyy-mm-dd"
' 3:"hh:mm:ss"
' 4:"yyyy年mm月dd日"
' 5:"yyyymmdd"
'   6:"yyyymmddhhmmss" 
' ============================================
Function Format_Time(s_Time, n_Flag)
Dim y, m, d, h, mi, s
Format_Time = ""
If IsDate(s_Time) = False Then Exit Function
y = cstr(year(s_Time))
m = cstr(month(s_Time))
If len(m) = 1 Then m = "0" & m
d = cstr(day(s_Time))
If len(d) = 1 Then d = "0" & d
h = cstr(hour(s_Time))
If len(h) = 1 Then h = "0" & h
mi = cstr(minute(s_Time))
If len(mi) = 1 Then mi = "0" & mi
s = cstr(second(s_Time))
If len(s) = 1 Then s = "0" & s
Select Case n_Flag
Case 1
  ' yyyy-mm-dd hh:mm:ss
  Format_Time = y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
Case 2
  ' yyyy-mm-dd
  Format_Time = y & "-" & m & "-" & d
Case 3
  ' hh:mm:ss
  Format_Time = h & ":" & mi & ":" & s
Case 4
  ' yyyy年mm月dd日
  Format_Time = y & "年" & m & "月" & d & "日"
Case 5
  ' yyyymmdd
  Format_Time = y & m & d
case 6
  'yyyymmddhhmmss
  format_time= y & m & d & h & mi & s
End Select
End Function
%>

<%=format_time(now,1)%>

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com