?Asp基本日期函數 以及 日期函數擴展類(原創)
clsDateFunEx_Power by Sman & Net Fetch:
下載文件點擊下載此文件

Asp基本日期函數:
函數 語法 說明 示例
Now Now() 取得系統當前的日期和時間
Date Date() 取得系統當前的日期
Time Time() 取得系統當前的時間
Year Year(Date) 取得給定日期的年份
Month Month(Date) 取得給定日期的月份
Day Day(Date) 取得給定日期是幾號
Hour Hour(time) 取得給定時間是第幾小時
Minute Minute(time) 取得給定時間是第幾分鐘
Second Second(time) 取得給守時間是第幾秒
WeekDay WeekDay(Date) 取得給定日期是 星期幾的整數,1表示星期日,2表示星期一,依此類推
DateDiff("Var",Var1,Var2)
Var:日期或時間間隔因子,有如下參數:yyyy 年 m月 d 日 ww星期 h小時 s秒
Var1:第一個日期或時間
Var2:第二個日期或時間,比Var1晚 計算兩個日期或時間的間隔

DateAdd("Var",Var1,Var2)
Var:日期或時間間隔因子:
Var1:日期或時間間隔倍數
Var2:日期或時間的基準 對兩個日期或時間作加法
如果計算的日期是在公元 100 年之前,則會產生錯誤。

FormatDateTime FormatDateTime(Date,vbShortDate) 轉化為短日期格式 FromatDateTime(Date(),vbLongDate) "以長日期格式顯示
FormatDateTime(Date,vbLongDate) 轉化為長日期格式
FormatDateTime(Date,vbShortTime) 轉化為短時間格式
FormatDateTime(Date,vbLongTime) 轉化為長時間格式

日期函數擴展類代碼 (clsDateFunEx_Power by Sman & Net Fetch):

程序代碼 程序代碼
<%
'轉發時請保留此聲明信息,這段聲明不并會影響你的速度!
'************************** 【日期擴展類】Ver 0.1.0 ********************************
'開發人:??Sman、Net Fetch
'開發日期:??2005-11-11
'版本號:??Ver 0.1.0

'官方網站:http://www.sman.cnhttp://www.ad0.cn
'電子郵件:huihui3030@126.com、Ad0@Ad0.Cn QQ:19341293 32050450
'版權聲明:版權沒有,盜版不究,源碼公開,歡迎盜版,歡迎你到官方網站來尋求支持。
'如有任何改進之處,麻煩轉發或者反饋一份到 huihui3030@126.com、Ad0@Ad0.Cn,Thanks!
'詳細使用說明或范例請見下載附件或到官方站點或Email聯系下載!
'************************************************************************************

Class DateFunEx

Private d_

?Private Sub class_initialize()
??d_ = ""
?End Sub
?
Public Property Let setDate(strDate) '傳入日期參數 strDate
d_ = strDate
End Property
?
?Public Property Get Version
??Version = "Copyright ? Date Function Extend Ver 0.1.0<br />" & _
????"Power by <a href='http://www.sman.cn' target='_blank'>Sman</a> " & _
????" & <a href='mailto:NetFetchStudio@163.com' target='_blank'>Net Fetch</a>"
?End Property

Sub FormatDate()
??On Error Resume Next
??If IsNumeric(d_) Then
???d_ = Cint(d_)
???If len(d_)< 3 Then d_ = "20" & right("0"&d_,2)
???d_ = d_ & "-1"
??End If
??d_ = cDate(d_)
End Sub??
'------------------------------
' 功能說明:算第幾周的星期幾是幾號
' 參數說明:y 年,w周,week 星期 (星期一1 星期天7) FirstDayofWeek 每周的第一天(詳細設置請參照VBS手冊)
' 例 2005年40周星期天 GetWeekDate(2005,40,7)
'------------------------------
Public Function GetWeekDate(y, w, DayofWeek)
??Call FormatDate()
Dim NewYearDay,FirstDayofWeek
FirstDayofWeek = 2
NewYearDay = CDate(y & "-1-1") '元旦
GetWeekDate = ((NewYearDay - Weekday(NewYearDay, FirstDayofWeek)) + (w - 1) * 7 + DayofWeek)
?End Function

'------------------------------
' 功能說明:獲得某年某月的天數
' 參數說明:d_ 年-月-日
' 例 2005年10月 GetMonthDayCount("2005-10-11")(日可要可不要)
'------------------------------
Public Function GetMonthDayCount()
??Call FormatDate()
GetMonthDayCount = DateDiff("d", d_, DateAdd("m", 1, d_))
End Function

'------------------------------
' 功能說明:得到某年某月的第一天
' 相關函數:GetMonthFirstDay
' 例 本月 GetMonthFirstDay(date)(日可要可不要) 上月 GetMonthFirstDay(dateadd("m",-1,date)) 以此類推
'------------------------------
Public Function GetMonthFirstDay()
??Call FormatDate()
GetMonthFirstDay = CDate( Year(d_) & "-" & Month(d_) & "-1")
End Function
?
'------------------------------
' 功能說明:得到某年的某月的最后一天
' 參數說明:d_ 年-月-日
' 關聯函數:GetMonthDayCount
' 例 本月 GetMonthLastDay(date)(日可要可不要) 上月 GetMonthLastDay(dateadd("m",-1,date)) 以此類推
'------------------------------
Public Function GetMonthLastDay()
??Call FormatDate()
GetMonthLastDay = CDate( Year(d_) & "-"&Month(d_) & "-" & DateDiff("d", d_, DateAdd("m", 1, d_)))
End Function

'------------------------------
' 功能說明:某日所在的周的第一天的日期
' 相關函數:GetWeekDate
' 例 本周 WeekFirstDay(date) 上周 WeekFirstDay(dateadd("ww",-1,date)) 以此類推
'------------------------------
Public Function WeekFirstDay()
??Call FormatDate()
WeekFirstDay = GetWeekDate(Year(d_), DatePart("ww", d_), 1)
End Function

'------------------------------
' 功能說明:某日所在的周的第最后一天的日期
' 相關函數:GetWeekDate
' 例 本周 WeekLastDay(date) 上周 WeekLastDay(dateadd("ww",-1,date)) 以此類推
'------------------------------
Public Function WeekLastDay()
??Call FormatDate()
WeekLastDay = GetWeekDate(Year(d_), DatePart("ww", d_), 7)
End Function
?
End Class
%>


調用代碼:
程序代碼 程序代碼
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<% Option Explicit %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Test_clsDateFunEx</title>
</head>

<body>
<!--#include file="clsDateFunEx.asp" -->
<%
Dim myDateFun,strDate
strDate = "2005-6"
Set myDateFun = new DateFunEx
?myDateFun.setDate = strDate
?Response.write "2005年第5周的星期天是幾號:<br>" & _
??????String(20, " ") & myDateFun.GetWeekDate(2005,10,7) &"<br>"
?Response.Write "本月的天數:<br>"&_
??????String(20, " ") & myDateFun.GetMonthDayCount & "<br>"
?Response.Write "本月的第一天:<br>"&_
??????String(20, " ") & myDateFun.GetMonthFirstDay & "<br>"
?myDateFun.setDate = Now()
?Response.Write "本月的最后一天:<br>"&_
??????String(20, " ") & myDateFun.GetMonthLastDay & "<br>"
?Response.Write "本月所在的周的第一天的日期:<br>"&_
??????String(20, " ") & myDateFun.WeekFirstDay & "<br>"
?Response.Write "本月所在的周的第最后一天的日期:<br>" & _
??????String(20, " ") & myDateFun.WeekLastDay & "<br>"
?Response.Write "<br><br><br><div style='padding-left:200px;font-size:12px;'>" & myDateFun.Version & "</div><br>"
Set myDateFun = Nothing
%>
</body>
</html>


程序代碼 程序代碼
'*************************************
'日期轉換函數
'*************************************
Function DateToStr(DateTime,ShowType)
?Dim DateMonth,DateDay,DateHour,DateMinute,DateWeek,DateSecond
?Dim FullWeekday,shortWeekday,Fullmonth,Shortmonth,TimeZone1,TimeZone2
?TimeZone1="+0800"
?TimeZone2="+08:00"
?FullWeekday=Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
?shortWeekday=Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
Fullmonth=Array("January","February","March","April","May","June","July","August","September","October","November","December")
Shortmonth=Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

?DateMonth=Month(DateTime)
?DateDay=Day(DateTime)
?DateHour=Hour(DateTime)
?DateMinute=Minute(DateTime)
?DateWeek=weekday(DateTime)
?DateSecond=Second(DateTime)
?If Len(DateMonth)<2 Then DateMonth="0"&DateMonth
?If Len(DateDay)<2 Then DateDay="0"&DateDay
?If Len(DateMinute)<2 Then DateMinute="0"&DateMinute
?Select Case ShowType
?Case "Y-m-d"
??DateToStr=Year(DateTime)&"-"&DateMonth&"-"&DateDay
?Case "Y-m-d H:I A"
??Dim DateAMPM
??If DateHour>12 Then
???DateHour=DateHour-12
???DateAMPM="PM"
??Else
???DateHour=DateHour
???DateAMPM="AM"
??End If
??If Len(DateHour)<2 Then DateHour="0"&DateHour?
??DateToStr=Year(DateTime)&"-"&DateMonth&"-"&DateDay&" "&DateHour&":"&DateMinute&" "&DateAMPM
?Case "Y-m-d H:I:S"
??If Len(DateHour)<2 Then DateHour="0"&DateHour?
??If Len(DateSecond)<2 Then DateSecond="0"&DateSecond
??DateToStr=Year(DateTime)&"-"&DateMonth&"-"&DateDay&" "&DateHour&":"&DateMinute&":"&DateSecond
?Case "YmdHIS"
??DateSecond=Second(DateTime)
??If Len(DateHour)<2 Then DateHour="0"&DateHour?
??If Len(DateSecond)<2 Then DateSecond="0"&DateSecond
??DateToStr=Year(DateTime)&DateMonth&DateDay&DateHour&DateMinute&DateSecond?
?Case "ym"
??DateToStr=Right(Year(DateTime),2)&DateMonth
?Case "d"
??DateToStr=DateDay
Case "ymd"
DateToStr=Right(Year(DateTime),4)&DateMonth&DateDay
Case "mdy"
Dim DayEnd
select Case DateDay
Case 1
DayEnd="st"
Case 2
DayEnd="nd"
Case 3
DayEnd="rd"
Case Else
DayEnd="th"
End Select
DateToStr=Fullmonth(DateMonth-1)&" "&DateDay&DayEnd&" "&Right(Year(DateTime),4)
Case "w,d m y H:I:S"
??DateSecond=Second(DateTime)
??If Len(DateHour)<2 Then DateHour="0"&DateHour?
??If Len(DateSecond)<2 Then DateSecond="0"&DateSecond
DateToStr=shortWeekday(DateWeek-1)&","&DateDay&" "& Left(Fullmonth(DateMonth-1),3) &" "&Right(Year(DateTime),4)&" "&DateHour&":"&DateMinute&":"&DateSecond&" "&TimeZone1
Case "y-m-dTH:I:S"
??If Len(DateHour)<2 Then DateHour="0"&DateHour?
??If Len(DateSecond)<2 Then DateSecond="0"&DateSecond
??DateToStr=Year(DateTime)&"-"&DateMonth&"-"&DateDay&"T"&DateHour&":"&DateMinute&":"&DateSecond&TimeZone2
?Case Else
??If Len(DateHour)<2 Then DateHour="0"&DateHour
??DateToStr=Year(DateTime)&"-"&DateMonth&"-"&DateDay&" "&DateHour&":"&DateMinute
?End Select
End Function