木子屋 Dnawo's BLOG

判断某月份总天数

👤 dnawo 📅 2006-12-26 👁 7580 👍 0 💬 0 🔄 来源:本站原创
在制作万年历时首先要解决的一个问题是:如何判断某年某月总天数。

1.方法一

由于每月天数的范围为[28,31],所以可以依次从31到28,利用isdate函数判断是否为合法日期,首次true则该值就为当月天数;当然也可以依次从28到31,同样利用isdate判断是否为合法日期,首次false则该值减1就为当月天数,实现函数分别为:

Function Getnum(iyear,imonth)
	Dim i,idate
	For i=31 to 28 step -1
		idate = iyear & "-" & imonth & "-" & i
		if isdate(idate) then
			Getnum = i
			Exit function
		End if
	Next
End function




Function Getnum(iyear,imonth)
	Dim i,idate
	For i=28 to 32
		idate = iyear & "-" & imonth & "-" & i
		if not(isdate(idate)) then
			Getnum = i - 1
			Exit function
		End if
	Next
End function


想想哪个更好点^_^

2.方法二

本方法是通过对月份天数进行观察而总结出规律:1,3,5,7,8,10,12为31天;4,6,9,11为20天;2月有时为28天,有时为29天。实现函数为:

Function Getnum(iYear,iMonth)
	Select Case iMonth
		Case 1, 3, 5, 7, 8, 10, 12
			Getnum = 31
		Case 4, 6, 9, 11
			Getnum = 30
		Case 2
			If IsDate(iyear & "-" & imonth & "-29") Then
				Getnum = 29
			Else
				Getnum = 28
			End If
	End Select
End Function


3.附date.asp,使用方法一显示指定年份各月份天数,便于观察

<%
Rem 取得月份天数
Function Getnum(iyear,imonth)
	Dim i,idate
	For i=31 to 28  step -1
		idate = iyear & "-" & imonth & "-" & i
		if isdate(idate) then
			Getnum = i
			Exit function
		End if
	Next
End function

Rem 标记不同天数颜色,方便浏览
Function Numcolor(n)
	Select case n
		case 28
			Numcolor="<b><font color=red>" & n & "</font></b>"
		case 29
			Numcolor="<b><font color=blue>" & n & "</font></b>"
		case 30
			Numcolor="<b><font color=black>" & n & "</font></b>"
		case 31
			Numcolor="<b><font color=green>" & n & "</font></b>"
	End select
End function

Dim n,j,k
Rem 1.表头显示1月到12月
Response.write "<table width='750' border='0' cellspacing='0' cellpadding='0'>" & vbcrlf
Response.write "  <tr>" & vbcrlf
Response.write "    <td width='50' height='20' align='center' valign='middle'> </td>" & vbcrlf
For n=1 to 12
	Response.write "    <td width='50' height='20' align='center' valign='middle'>" & n & "月</td>" & vbcrlf
Next
Response.write "  </tr>" & vbcrlf
Rem 2.显示各年份中每月天数,本例年份从500至600
For j=500 to 600
	Response.write "  <tr>" & vbcrlf
	Response.write "    <td width='50' height='20' align='center' valign='middle'>" & j & "</td>" & vbcrlf
	For k=1 to 12
		Response.write "    <td width='50' height='20' align='center' valign='middle'>" & Numcolor(Getnum(j,k)) & "</td>" & vbcrlf
	Next
	Response.write "  </tr>" & vbcrlf
Next
Rem 3.表格结尾标识
Response.write "</table>"
%>

评论(0)

暂无评论。

验证码
评论需审核通过后显示
← 上一篇 实现四台Web服务器的负载均衡 下一篇 → 一个简单的万年历