您的位置:首页 > 编程语言 > VB

格式化时间日期函数

2006-06-05 10:05 232 查看
<script language="vbScript">
<!--
Function DateTimeToString(oDate, sFormatInfo)
 Dim wkDayShort, wkDayLong
 Dim mtNameShort,mtNameLong
 wkDayShort = Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
 wkDayLong = Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
 mtNameShort = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
 mtNameLong = Array("January","February","March","April","May","June","July","August","September","October","November","December")
 '格式模式替换
 sFormatInfo = Replace(sFormatInfo,"yyyy",Year(oDate))
 '临时转义
 sFormatInfo = Replace(sFormatInfo,"MMMM","$1")
 sFormatInfo = Replace(sFormatInfo,"dddd","$2")
 sFormatInfo = Replace(sFormatInfo,"MMM","$3")
 sFormatInfo = Replace(sFormatInfo,"ddd","$4")

 sFormatInfo = Replace(sFormatInfo,"yy",Right(Year(oDate),2))
 sFormatInfo = Replace(sFormatInfo,"MM",Right(String(2,"0") & Month(oDate),2))
 sFormatInfo = Replace(sFormatInfo,"dd",Right(String(2,"0") & Day(oDate),2))
 sFormatInfo = Replace(sFormatInfo,"HH",Right(String(2,"0") & Hour(oDate),2))
 sFormatInfo = Replace(sFormatInfo,"mm",Right(String(2,"0") & Minute(oDate),2))
 sFormatInfo = Replace(sFormatInfo,"ss",Right(String(2,"0") & Second(oDate),2))
 sFormatInfo = Replace(sFormatInfo,"M",Right(Month(oDate),1))
 sFormatInfo = Replace(sFormatInfo,"d",Right(Day(oDate),1))
 sFormatInfo = Replace(sFormatInfo,"h",Right(Day(oDate),1))
 sFormatInfo = Replace(sFormatInfo,"m",Right(Minute(oDate),1))

 sFormatInfo = Replace(sFormatInfo,"$1",mtNameLong(Month(oDate)-1))
 sFormatInfo = Replace(sFormatInfo,"$2",wkDayLong(Weekday(oDate,1)-1))
 sFormatInfo = Replace(sFormatInfo,"$3",mtNameShort(Month(oDate)-1))
 sFormatInfo = Replace(sFormatInfo,"$4",wkDayShort(Weekday(oDate,1)-1))

 DateTimeToString = sFormatInfo
End Function
//-->
</script>

--------------------------------------
实现以下格式模式:(英文可以改为中文)

格式模式     说明
d   月中的某一天。一位数的日期没有前导零。
dd   月中的某一天。一位数的日期有一个前导零。
ddd   周中某天的缩写名称,定义范围: "Sun","Mon","Tue","Wed","Thu","Fri","Sat"
dddd   周中某天的完整名称,定义范围: "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"。
M   月份数字。一位数的月份没有前导零。
MM   月份数字。一位数的月份有一个前导零。
MMM   月份的缩写名称,定义范围: "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"。
MMMM   月份的完整名称,定义范围: "January","February","March","April","May","June","July","August","September","October","November","December"。 
yy   不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy   包括纪元的四位数的年份。   
H   24 小时制的小时。一位数的小时数没有前导零。
HH   24 小时制的小时。一位数的小时数有前导零。
m   分钟。一位数的分钟数没有前导零。
mm   分钟。一位数的分钟数有一个前导零。
s   秒。一位数的秒数没有前导零。
ss   秒。一位数的秒数有一个前导零。

实例:
-------------------
DateTimeToString(Now, "yyyy年M月dd日 HH:mm:ss")
->类似于 2006年5月22日 17:02:38
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string vbscript function sun