if (month in (1,3,5,7,8,10,12))
  return 31;
else if (month in(4,6,9,11))
  return 30;
else if (year 是闰年)
  return 29;
else
  return 28;
闰年的算法到处都有,就不写出来了。
闰年的算法
((year mod 4 = 0) and (year mod 100 <> 0)) or (year mod 400 = 0)---------------方法二
DateDiff("d",DateSerial(datYear,datMonth,1), DateAdd("m", 1,DateSerial(datYear,datMonth,1)))
其中datYear,datMonth是要计算的年和月。使用DateAdd可以保证计算12月时正确。

解决方案 »

  1.   

    反正只有12个月,你自己用个if组或者Select就行了吗。
    2月分的话用判断语句
    if (year mod 400 =0) and ((year mod 4=0) and (year mod 100<>0)) then text1.text=29
    else text1.text=28我记不清if语句使用begin、end的情况了,可能你要添加一两对Begin、End
      

  2.   

    Private Sub Command1_Click()
        If Not IsDate(Text1) Then MsgBox "日期格式错": Exit Sub
        MsgBox DateDiff("d", Text1, DateAdd("m", 1, Text1))
    End Sub
      

  3.   

    呵呵,sorry我把闰年的算法都打错了,不好意思。
      

  4.   

    上面的都不好,这个问题我熟悉,用vb自己的函数,根本不用计算什么闰年!
    '返回当前月长度
    Private Function Fun_GetMonthLen(iYear As String, iMonth As String) As Integer
        Dim sBeginDate As String, sEndDate As String
        sBeginDate = Format(Trim(iYear) + "-" + Trim(iMonth) + "-1", "yyyy-mm-dd")
        If iMonth + 1 > 12 Then
            sEndDate = Trim(iYear + 1) + "-1-1"
        Else
            sEndDate = Trim(iYear) + "-" + Trim(iMonth + 1) + "-1"
        End If
        sEndDate = Format(CDate(sEndDate), "yyyy-mm-dd")
        Fun_GetMonthLen = Val(DateDiff("d", sBeginDate, sEndDate))
    End Function
    这里我的输入参数是年月,如果你输入参数是日期,那么在函数里面将该日期的年月拆分出来就可以了
      

  5.   

    楼上的老兄的函数也行,与我的差不多,你可以比较看一下,那个对你更合适。
    思想是:知道了这个月之后,让月份加1然后从加1月份的1号 减1 .就行了大体上就这意思。
    <<函数1>>Function GetEndDay(startDate As String) As Date
    Dim m As String
    Dim ss As Date
    ss = FormatDateTime(startDate, vbLongDate)
    If CStr(Format(ss, "mm")) = "12" Then
      m = "01"
    Else
      m = CStr(Format(ss, "mm") + 1)
      If Len(m) < 2 Then
        m = "0" + m
      End If
    End If
    Do
     ss = ss + 1
     If CStr(Format(ss, "mm")) = m Then
     Exit Do
     End If
    Loop
    GetEndDay = FormatDateTime(ss - 1, vbLongDate)
    End Function
    <<函数2>>Private Function getLastDay(dtDate As Date) As String
    Dim intYear As Integer
    Dim intMonth As Integer
    Dim intDay As Integer
     
    intYear = Year(dtDate)
    intMonth = Month(dtDate)
     
    If intMonth < 12 Then
      intDay = Day(DateValue(str(intYear) + "/" + str(intMonth + 1) + "/01") - 1)
    Else
      intDay = 31
    End If
     getLastDay = Trim(str(intDay))
     
    End Function