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

所属分类: 网络编程 / ASP编程 阅读数: 1886
收藏 0 赞 0 分享

核心代码

<%
'功能:多功能日期格式化函数
'来源:http://jorkin.reallydo.com/article.asp?id=477

Function FormatDate(sDateTime, sReallyDo)
 Dim sJorkin
 sJorkin = GetLocale()
 If Not IsDate(sDateTime) Then sDateTime = Now()
 sDateTime = CDate(sDateTime)
 Select Case UCase(sReallyDo & "")
 Case "0", "1", "2", "3", "4"
  FormatDate = FormatDateTime(sDateTime, sReallyDo)
 Case "00"
  FormatDate = FormatDate(sDateTime, "YYYY-MM-DD hh:mm:ss")
 Case "01"
  FormatDate = FormatDate(sDateTime, "YYYY年MM月DD日")
 Case "02"
  FormatDate = FormatDate(sDateTime, "YYYY-MM-DD")
 Case "03"
  FormatDate = FormatDate(sDateTime, "hh:mm:ss")
 Case "04"
  FormatDate = FormatDate(sDateTime, "hh:mm")
 Case "ISO8601", "GOOGLE", "SITEMAP" '//ISO8601格式, 一般用于GoogleSiteMap, "+08:00" 为时区.
  FormatDate = FormatDate(sDateTime, "YYYY-MM-DDThh:mm:ss.000+08:00")
 Case "RFC822", "RSS", "FEED" '//RFC822格式, 一般用于RSS, "+0800" 为时区.
  SetLocale("en-gb")
  FormatDate = FormatDate(sDateTime, "ew, DD eMM YYYY hh:mm:ss +0800")
  SetLocale(sJorkin)
 Case "RND", "RAND", "RANDOMIZE" '//随机字符串
  Randomize
  sJorkin = Rnd()
  FormatDate = FormatDate(sDateTime, "YYYYMMDDhhmmss") & _
    Fix((9 * 10^6 -1) * sJorkin) + 10^6
 Case Else
  FormatDate = sReallyDo
  FormatDate = Replace(FormatDate, "YYYY", Year(sDateTime))
  FormatDate = Replace(FormatDate, "DD", Right("0" & Day(sDateTime), 2))
  FormatDate = Replace(FormatDate, "hh", Right("0" & Hour(sDateTime), 2))
  FormatDate = Replace(FormatDate, "mm", Right("0" & Minute(sDateTime), 2))
  FormatDate = Replace(FormatDate, "ss", Right("0" & Second(sDateTime), 2))
  FormatDate = Replace(FormatDate, "YY", Right(Year(sDateTime), 2))
  FormatDate = Replace(FormatDate, "D", Day(sDateTime))
  FormatDate = Replace(FormatDate, "h", Hour(sDateTime))
  FormatDate = Replace(FormatDate, "m", Minute(sDateTime))
  FormatDate = Replace(FormatDate, "s", Second(sDateTime))
  If InStr(1, FormatDate, "EW", 1) > 0 Then
  SetLocale("en-gb")
  FormatDate = Replace(FormatDate, "EW", UCase(WeekdayName(Weekday(sDateTime), False)))
  FormatDate = Replace(FormatDate, "eW", WeekdayName(Weekday(sDateTime), False))
  FormatDate = Replace(FormatDate, "Ew", UCase(WeekdayName(Weekday(sDateTime), True)))
  FormatDate = Replace(FormatDate, "ew", WeekdayName(Weekday(sDateTime), True))
  SetLocale(sJorkin)
  Else
  FormatDate = Replace(FormatDate, "W", WeekdayName(Weekday(sDateTime), False))
  FormatDate = Replace(FormatDate, "w", WeekdayName(Weekday(sDateTime), True))
  End If
  If InStr(1, FormatDate, "EMM", 1) > 0 Then
  SetLocale("en-gb")
  FormatDate = Replace(FormatDate, "EMM", MonthName(Month(sDateTime), False))
  FormatDate = Replace(FormatDate, "eMM", MonthName(Month(sDateTime), True))
  SetLocale(sJorkin)
  Else
  FormatDate = Replace(FormatDate, "MM", Right("0" & Month(sDateTime), 2))
  FormatDate = Replace(FormatDate, "M", Month(sDateTime))
  End If
 End Select
End Function
%>

用法如下:

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

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

更多精彩内容其他人还在看

把网页中的(电话,qq等数字)生成图片的ASP程序

下面的代码可以将网页中一些防止别人直接复制或采集的不错的隐藏部分数字信息,例如电话可以起到防止别人搜索。
收藏 0 赞 0 分享

asp生成不需要数据库的中奖码

有一个思路是:将一批唯一中奖码,录入的到数据库,中奖时,取出来一条,做一个标记,把中奖码告诉
收藏 0 赞 0 分享

asp遍历目录及子目录的函数

asp遍历目录以及目录下文件的函数- 从网上找的不过都用不了,自己用Emeditor修正了一下
收藏 0 赞 0 分享

asp数据库连接rs("user.id")

数据库连接,有时会出错在conn.open connstr这地方的问题,及联接查询调用rs("user.id")的问题
收藏 0 赞 0 分享

vbs或asp采集文章时网页编码问题

研究网页编码很长时间了,因为最近要设计一个友情链接检测的VBS脚本,而与你链接的人的页面很可能是各种编码
收藏 0 赞 0 分享

新手asp编程的基本法则与常见错误注意事项

在论坛看到很多帖子代码中都有一个共同的基本错误,字段类型错误。程序和数据库是紧紧相连的,数据库字段文本型或时间型的都使用单引号
收藏 0 赞 0 分享

提高SQL的执行效率的ASP的五种做法

我们用开发sql的时候会发现,有时候asp执行比较慢,我们可以用下面的方法,来提高执行效率
收藏 0 赞 0 分享

asp的offset的一个go to page第1/2页

呵,指定到第几页,用dw自带的生成的东西,我们可以参考他的思想,代码很多垃圾代码
收藏 0 赞 0 分享

asp中command的在单条记录时,有些字段显示为空的问题

网上找到一些资料,关于游标这个问题,还是无法放进去(如果方法找到的话,还可以改) 之前的sql这么写,但是没有效果有些非顺序的值会丢失
收藏 0 赞 0 分享

功能不错的asp模板类代码附下载第1/4页

在工作中,因为要用模板类,在网上找了个asp模板类,用着挺好了。完工后也按别人的思路写了模板类,这个模板类没在设计的项目中应用,只是练练笔,所以只写了简单的几个demo,有没有bug还不清楚,欢迎大家测试,指教,帮忙完善。
收藏 0 赞 0 分享
查看更多