判断某月份总天数

在制作万年历时首先要解决的一个问题是:如何判断某年某月总天数。

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>"
%>


上一篇: 实现四台Web服务器的负载均衡
下一篇: 一个简单的万年历
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 1 | 引用: 0 | 查看次数: 6988
发表评论
登录后再发表评论!