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