欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-26 08:37:08
核心代码 <% '功能:多功能日期格式化函数 '来源:http://jorkin.reallydo.com/article.asp?id=477...

核心代码

<%
'功能:多功能日期格式化函数
'来源: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"))
%>

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