网络编程
位置:首页>> 网络编程>> Asp编程>> asp日期时间格式化函数

asp日期时间格式化函数

作者:davc 来源:51js 发布时间:2009-12-14 12:56:00 

标签:日期,时间,格式化,函数,asp

时间格式化函数,代码简单但较实用

代码很简单,谁都能看懂


Function fmstr(str, str1, Lens)
Dim str2
For I = 1 To Lens - Len(str)
str2 = str2 & str1
Next
fmstr = str2 & str
End Function
Function formattime(times, str) '时间格式化(要格式化的时间,格式)
str=Replace(str,"yyyy",year(times))
str=Replace(str,"yy",right(year(times),2))
str=Replace(str,"mm",fmstr(month(times),0,2))
str=Replace(str,"m",month(times))
str=Replace(str,"dd",fmstr(Day(times),0,2))
str=Replace(str,"d",Day(times))
str=Replace(str,"hh",fmstr(Hour(times),0,2))
str=Replace(str,"h",Hour(times))
str=Replace(str,"nn",fmstr(Minute(times),0,2))
str=Replace(str,"n",Minute(times))
str=Replace(str,"ss",fmstr(Second(times),0,2))
str=Replace(str,"s",Second(times))
formattime=str
End Function

例:
formattime(now(), "yyyy-mm-dd hh:nn:ss")---2009-01-05 13:05:50
formattime(now(), "yy-m-d h:n:s")---09-1-5 13:5:50
formattime(now(), "mm-dd hh:nn")---01-05 13:05
formattime(now(), "yyyymmddhhnnss")---20090105130550
formattime(now(), "现在是yyyy年m月d日,当前时间:hh时nn分ss秒")---现在是2009年1月5日,当前时间:13时05分50秒   刚想到,如果有英文的话,会产生误替换,改了一下。。。。

Function fmstr(str)
if str<10 then
fmstr="0"&str
else
fmstr=str
end if
End Function
Function formattime(times, str) '时间格式化(要格式化的时间,格式)
str=Replace(str,"$yyyy$",year(times))
str=Replace(str,"$yy$",right(year(times),2))
str=Replace(str,"$mm$",fmstr(month(times)))
str=Replace(str,"$m$",month(times))
str=Replace(str,"$dd$",fmstr(Day(times)))
str=Replace(str,"$d$",Day(times))
str=Replace(str,"$hh$",fmstr(Hour(times)))
str=Replace(str,"$h$",Hour(times))
str=Replace(str,"$nn$",fmstr(Minute(times)))
str=Replace(str,"$n$",Minute(times))
str=Replace(str,"$ss$",fmstr(Second(times)))
str=Replace(str,"$s$",Second(times))
formattime=str
End Function

例:
formattime(now(), "$yyyy$-$mm$-$dd$ $hh$:nn$:$ss$")---2009-01-05 13:05:50
formattime(now(), "$yy$-$m$-$d$ $h$:$n$:$s$")---09-1-5 13:5:50
formattime(now(), "$mm$-$dd$ $hh$:$nn$")---01-05 13:05
formattime(now(), "$yyyy$$mm$$dd$$hh$$nn$$ss$")---20090105130550
formattime(now(), "现在是$yyyy$年$m$月$d$日,当前时间:$hh$时$nn$分$ss$秒")---现在是2009年1月5日,当前时间:13时05分50秒
formattime(now(), "year:$yyyy$,month:$m$,date:$d$,time:$hh$时$nn$分$ss$秒")

像最后一句,以原来的方法会把date中的d,month中的m等等替换掉


0
投稿

猜你喜欢

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