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

所属分类: 网络编程 / ASP编程 阅读数: 1871
收藏 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"))
%>

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

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

非常好用的asp备份,还原SQL数据库的代码

用asp的朋友,可以用下面的代码,实现mssql数据库的备份还原操作
收藏 0 赞 0 分享

asp从字符串里截取N个带HTML的字符的函数

从字符串里截取N个带HTML的字符,现在的实现方法还不完善,过程是:从字符串里找各种控件的开始,然后依些分开,放到数组里,然后一个一个数组的加上来看看字符是否大于给定的字符如果是,那么,返回这个数组
收藏 0 赞 0 分享

asp下Response.Buffer提速

用Response.Buffer=True为程序加速,Response.Flush()内容至少要有256字节
收藏 0 赞 0 分享

ASP,vbs正则轮翻在文章段落后加上网址等内容

ASP,vbs正则轮翻在文章段落后加上网址,网站名称,网站介绍等内容
收藏 0 赞 0 分享

ASP为字符串中的网址自动加上链接

asp下用正则实现字符串中的网址加链接的代码
收藏 0 赞 0 分享

ASP vbs 代码大小写规范

asp大小写规范是个网页转换工具,实现asp中大小写的替换,大家可以测试下
收藏 0 赞 0 分享

ASP实现智能搜索实现代码

asp下智能搜索功能的实现,方便大家学习
收藏 0 赞 0 分享

asp结合fso实现文件或文件夹创建删除等操作的函数

sp+fso实现文件或文件夹检测是否存在,创建,删除的函数
收藏 0 赞 0 分享

asp下对POST提交数据限制的解决方法

由于微软对POST提交数据的大小进行了限制,当表单发送的数据量很大时,就会报错(错误 "ASP 0107 : 80004005")。原因是微软对用Request.Form()可接收的最大数据限制为100K字节。
收藏 0 赞 0 分享

asp+Access通用的自动替换数据库中的字符串

几年前写的一个脚本,可以自动进行整库的数据替换。
收藏 0 赞 0 分享
查看更多