?Asp基本日期函數(shù) 以及 日期函數(shù)擴(kuò)展類(原創(chuàng))
clsDateFunEx_Power by Sman & Net Fetch:
下載文件點(diǎn)擊下載此文件

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

DateAdd("Var",Var1,Var2)
Var:日期或時(shí)間間隔因子:
Var1:日期或時(shí)間間隔倍數(shù)
Var2:日期或時(shí)間的基準(zhǔn) 對(duì)兩個(gè)日期或時(shí)間作加法
如果計(jì)算的日期是在公元 100 年之前,則會(huì)產(chǎn)生錯(cuò)誤。

FormatDateTime FormatDateTime(Date,vbShortDate) 轉(zhuǎn)化為短日期格式 FromatDateTime(Date(),vbLongDate) "以長(zhǎng)日期格式顯示
FormatDateTime(Date,vbLongDate) 轉(zhuǎn)化為長(zhǎng)日期格式
FormatDateTime(Date,vbShortTime) 轉(zhuǎn)化為短時(shí)間格式
FormatDateTime(Date,vbLongTime) 轉(zhuǎn)化為長(zhǎng)時(shí)間格式

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

程序代碼 程序代碼
<%
'轉(zhuǎn)發(fā)時(shí)請(qǐng)保留此聲明信息,這段聲明不并會(huì)影響你的速度!
'************************** 【日期擴(kuò)展類】Ver 0.1.0 ********************************
'開(kāi)發(fā)人:??Sman、Net Fetch
'開(kāi)發(fā)日期:??2005-11-11
'版本號(hào):??Ver 0.1.0

'官方網(wǎng)站:http://www.sman.cnhttp://www.ad0.cn
'電子郵件:huihui3030@126.com、Ad0@Ad0.Cn QQ:19341293 32050450
'版權(quán)聲明:版權(quán)沒(méi)有,盜版不究,源碼公開(kāi),歡迎盜版,歡迎你到官方網(wǎng)站來(lái)尋求支持。
'如有任何改進(jìn)之處,麻煩轉(zhuǎn)發(fā)或者反饋一份到 huihui3030@126.com、Ad0@Ad0.Cn,Thanks!
'詳細(xì)使用說(shuō)明或范例請(qǐng)見(jiàn)下載附件或到官方站點(diǎn)或Email聯(lián)系下載!
'************************************************************************************

Class DateFunEx

Private d_

?Private Sub class_initialize()
??d_ = ""
?End Sub
?
Public Property Let setDate(strDate) '傳入日期參數(shù) 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??
'------------------------------
' 功能說(shuō)明:算第幾周的星期幾是幾號(hào)
' 參數(shù)說(shuō)明:y 年,w周,week 星期 (星期一1 星期天7) FirstDayofWeek 每周的第一天(詳細(xì)設(shè)置請(qǐng)參照VBS手冊(cè))
' 例 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

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

'------------------------------
' 功能說(shuō)明:得到某年某月的第一天
' 相關(guān)函數(shù):GetMonthFirstDay
' 例 本月 GetMonthFirstDay(date)(日可要可不要) 上月 GetMonthFirstDay(dateadd("m",-1,date)) 以此類推
'------------------------------
Public Function GetMonthFirstDay()
??Call FormatDate()
GetMonthFirstDay = CDate( Year(d_) & "-" & Month(d_) & "-1")
End Function
?
'------------------------------
' 功能說(shuō)明:得到某年的某月的最后一天
' 參數(shù)說(shuō)明:d_ 年-月-日
' 關(guān)聯(lián)函數(shù):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

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

'------------------------------
' 功能說(shuō)明:某日所在的周的第最后一天的日期
' 相關(guān)函數(shù):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
%>


調(diào)用代碼:
程序代碼 程序代碼
<%@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周的星期天是幾號(hào):<br>" & _
??????String(20, " ") & myDateFun.GetWeekDate(2005,10,7) &"<br>"
?Response.Write "本月的天數(shù):<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>


程序代碼 程序代碼
'*************************************
'日期轉(zhuǎn)換函數(shù)
'*************************************
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