系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 网络编程 >  ASP编程 > 详细页面

ASP常用日期格式化函数 FormatDate()

时间:2020-02-03来源:系统城作者:电脑系统城

核心代码


 
  1. <%
  2. '功能:多功能日期格式化函数
  3. '来源:http://jorkin.reallydo.com/article.asp?id=477
  4.  
  5. Function FormatDate(sDateTime, sReallyDo)
  6. Dim sJorkin
  7. sJorkin = GetLocale()
  8. If Not IsDate(sDateTime) Then sDateTime = Now()
  9. sDateTime = CDate(sDateTime)
  10. Select Case UCase(sReallyDo & "")
  11. Case "0", "1", "2", "3", "4"
  12. FormatDate = FormatDateTime(sDateTime, sReallyDo)
  13. Case "00"
  14. FormatDate = FormatDate(sDateTime, "YYYY-MM-DD hh:mm:ss")
  15. Case "01"
  16. FormatDate = FormatDate(sDateTime, "YYYY年MM月DD日")
  17. Case "02"
  18. FormatDate = FormatDate(sDateTime, "YYYY-MM-DD")
  19. Case "03"
  20. FormatDate = FormatDate(sDateTime, "hh:mm:ss")
  21. Case "04"
  22. FormatDate = FormatDate(sDateTime, "hh:mm")
  23. Case "ISO8601", "GOOGLE", "SITEMAP" '//ISO8601格式, 一般用于GoogleSiteMap, "+08:00" 为时区.
  24. FormatDate = FormatDate(sDateTime, "YYYY-MM-DDThh:mm:ss.000+08:00")
  25. Case "RFC822", "RSS", "FEED" '//RFC822格式, 一般用于RSS, "+0800" 为时区.
  26. SetLocale("en-gb")
  27. FormatDate = FormatDate(sDateTime, "ew, DD eMM YYYY hh:mm:ss +0800")
  28. SetLocale(sJorkin)
  29. Case "RND", "RAND", "RANDOMIZE" '//随机字符串
  30. Randomize
  31. sJorkin = Rnd()
  32. FormatDate = FormatDate(sDateTime, "YYYYMMDDhhmmss") & _
  33. Fix((9 * 10^6 -1) * sJorkin) + 10^6
  34. Case Else
  35. FormatDate = sReallyDo
  36. FormatDate = Replace(FormatDate, "YYYY", Year(sDateTime))
  37. FormatDate = Replace(FormatDate, "DD", Right("0" & Day(sDateTime), 2))
  38. FormatDate = Replace(FormatDate, "hh", Right("0" & Hour(sDateTime), 2))
  39. FormatDate = Replace(FormatDate, "mm", Right("0" & Minute(sDateTime), 2))
  40. FormatDate = Replace(FormatDate, "ss", Right("0" & Second(sDateTime), 2))
  41. FormatDate = Replace(FormatDate, "YY", Right(Year(sDateTime), 2))
  42. FormatDate = Replace(FormatDate, "D", Day(sDateTime))
  43. FormatDate = Replace(FormatDate, "h", Hour(sDateTime))
  44. FormatDate = Replace(FormatDate, "m", Minute(sDateTime))
  45. FormatDate = Replace(FormatDate, "s", Second(sDateTime))
  46. If InStr(1, FormatDate, "EW", 1) > 0 Then
  47. SetLocale("en-gb")
  48. FormatDate = Replace(FormatDate, "EW", UCase(WeekdayName(Weekday(sDateTime), False)))
  49. FormatDate = Replace(FormatDate, "eW", WeekdayName(Weekday(sDateTime), False))
  50. FormatDate = Replace(FormatDate, "Ew", UCase(WeekdayName(Weekday(sDateTime), True)))
  51. FormatDate = Replace(FormatDate, "ew", WeekdayName(Weekday(sDateTime), True))
  52. SetLocale(sJorkin)
  53. Else
  54. FormatDate = Replace(FormatDate, "W", WeekdayName(Weekday(sDateTime), False))
  55. FormatDate = Replace(FormatDate, "w", WeekdayName(Weekday(sDateTime), True))
  56. End If
  57. If InStr(1, FormatDate, "EMM", 1) > 0 Then
  58. SetLocale("en-gb")
  59. FormatDate = Replace(FormatDate, "EMM", MonthName(Month(sDateTime), False))
  60. FormatDate = Replace(FormatDate, "eMM", MonthName(Month(sDateTime), True))
  61. SetLocale(sJorkin)
  62. Else
  63. FormatDate = Replace(FormatDate, "MM", Right("0" & Month(sDateTime), 2))
  64. FormatDate = Replace(FormatDate, "M", Month(sDateTime))
  65. End If
  66. End Select
  67. End Function
  68. %>
  69.  

用法如下:


 
  1. <%
  2. '将日期格式化为ISO8601格式
  3. Response.Write(FormatDate("2008-03-06 08:03:06", "SITEMAP"))
  4. '将日期格式化为RFC822格式
  5. Response.Write(FormatDate("2008-03-06 08:03:06", "RSS"))
  6. '将日期格式化为(英星期, 英月/日/年)
  7. Response.Write(FormatDate(Now(), "eW, EMM/DD/YYYY"))
  8. '将生成一个以年月日时分秒随机数的字符串
  9. Response.Write(FormatDate(Now(), "RND"))
  10. %>

更多的可以查看下面的相关文章

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载