ASP中通过该日历算法实现的具体代码
2018-09-06 11:56
  复制代码 代码如下:
<style> 
td{font-family:宋体;font-size:9pt} 
</style> 
<bodybgcolor=eeeeee> 
<tablewidth=180cellpadding=0cellspacing=1bgcolor=ddddddalign=center> 
<% 
以下为ASP中通过该日历算法实现的具体代码 
先判断是否指定了一个年份和月份,没有则根据当前的年和月份显示 
IfRequest(ReqDate)=then 
CurrentDate=Date 
else 
CurrentDate=Trim(Request(ReqDate)) 
endif 
pyear=year(CurrentDate) 
pmonth=month(CurrentDate) 
以下的代码生成日历显示的表格头内容 
%> 
<tralign=LEFTbgcolor=#dddddd> 
<tdwidth=14%height=19align=center> 
<inputtype=buttonvalue=<<onclick=JavaScript:location.href=?ReqDate=<%=DateAdd(m,-1,CurrentDate)%>> 
</td> 
<tdcolspan=5align=center> 
<%=pyear%>年<%=pmonth%>月 
</td> 
<tdwidth=14%align=center> 
<inputtype=buttonvalue=>>onclick=JavaScript:location.href=?ReqDate=<%=DateAdd(m,1,CurrentDate)%>> 
</td> 
</tr> 
<tralign=centerbgcolor=#CCCCCC> 
<tdwidth=14%height=19>日</td> 
<tdwidth=14%>一</td> 
<tdwidth=14%>二</td> 
<tdwidth=14%>三</td> 
<tdwidth=14%>四</td> 
<tdwidth=14%>五</td> 
<tdwidth=14%>六</td> 
</tr> 
<tralign=centerbgcolor=ffffffheight=19> 
<% 
由于ASP中没有获取指定月共有多少天的函数,因此我们需要通过其他算法来获得,算法其实很简单,就是计算一下要显示月份的1日至下个月的1日一共相差几天 
fromDate=FormatDateTime(month(CurrentDate)&/1/&year(CurrentDate)) 
toDate=FormatDateTime(DateAdd(m,1,fromDate)) 
获得要显示月份的第一天为周几 
nunmonthstart=weekday(fromDate)-1 
获得要显示的1日至下个月的1日一共相差几天(月份一共有多少天) 
nunmonthend=DateDiff(d,fromDate,toDate) 
判断显示日历需要用几行表格来显示(每行显示7天) 
ifnunmonthstart+nunmonthend<36then 
maxi=36 
else 
maxi=43 
endif 
循环生成表格并显示 
i=1 
dowhilei<maxi 
iv=i-nunmonthstart 
ifi>nunmonthstartandi<=nunmonthend+nunmonthstartthen 
如果为显示的是今天则用红色背景显示 
ifiv=Day(now)andmonth(now)=pmonthandyear(now)=pyearthen 
response.write(<tdalign=centerbgcolor=#ffaaaa><ahref=#>&iv&</a></td>) 
else 
response.write(<tdalign=center><ahref=#>&iv&</a></td>) 
endif 
else 
response.write(<td></td>) 
endif 
如果能被7整除(每行显示7个)则输出一个换行 
ifimod7=0then 
response.write(</tr><tralign=centerbgcolor=ffffffheight=19>) 
endif 
i=i+1 
loop 
%> 
</table> 
</body></html>