1. 程式人生 > >格式化時間日期函式

格式化時間日期函式

<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