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月时正确。
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月时正确。
解决方案 »
- vb 用 JMail 出错求教高手!!!!
- 關於msflexgrid的問題?
- 格式票据打印的问题
- VB6.0应用程序错误
- 一个多页的WORD文档,如何将它的每一页单独保存为一个DOC文件
- [求助]在线等高手帮忙编个数字统计软件
- 听别人说VB6的报表设计器不能设置打印非A4的纸张和横打设置,是真的吗?如果是,我怎么办呀?
- 求FoxBase.
- 哪里有Crystal Report7.0以上版本?最好有中文技术资料。
- 如果我用open的方法去打开一个图象文件.怎么知道文件已经打开.急
- 有谁能告诉我如何用VB制作包含主表子表的报表?
- 请问这里有专家分、可用分和参与分,我给别人分是将哪个分给别人?是可用分吗?
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
If Not IsDate(Text1) Then MsgBox "日期格式错": Exit Sub
MsgBox DateDiff("d", Text1, DateAdd("m", 1, Text1))
End Sub
'返回当前月长度
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
这里我的输入参数是年月,如果你输入参数是日期,那么在函数里面将该日期的年月拆分出来就可以了
思想是:知道了这个月之后,让月份加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