day(datediff(..)) if day=30 then msgbox "30 days" if day=31 .. end if if day=28 .. end if end if
给你个好用的。 不过,本人出于人道主义事先声明:如果你无聊的时候想去琢磨MonthDay函数是怎么写出来的,要有吐血身亡的心理准备。除了KiteGirl这种一万年不出一个的怪物,没有谁会BT到用这么怪的方式去使用VB的,因为它违背了VB这种语言的初衷,虽然的确比较爽快。Private Sub Form_Load() For I = 1 To 12 Debug.Print I, MonthDay(I, True) Next End SubFunction MonthDay(ByVal pMonth As Byte, Optional pBissextile As Boolean = False, Optional ByVal pDayList As String = "303232332323", Optional ByVal pListPatch As Byte = 20) As Byte 'pMonth 月份 'pBissextile 闰年(如果你查询的月份是闰年的,使它为真。否则可以缺省。) 'pDayList 天数变动表(默认即可) 'pListPatch 递减补偿值(默认即可)
Dim tOutValue As Byte
Dim tBytes() As Byte
tBytes() = pDayList tBytes(2) = tBytes(2) + (pBissextile And 1)
Dim tBytesIndex As Byte
tBytesIndex = (pMonth - 1) * 2
tOutValue = tBytes(tBytesIndex) - pListPatch
MonthDay = tOutValue End Function
方法1: (一行搞定,這里是指當前月份,如果要其它月份,把Date換成其它的日期就好.) Day(DateAdd("d", -1, Year(DateAdd("M", 1, Date)) & "-" & Month(DateAdd("M", 1, Date)) & "-1"))方法2:(子程序搞定)function GetDay(Lint_Date as Date) as integer 'Lint_Date為帶年月的日期型數據,也可以自己改成其它的, '返回日期數,整型 dim Lint_Month as integer dim Lint_Year as integer Lint_Month=Month(Lint_Date) Lint_Year=Year(Lint_Date) select case Lint_Month case 1,3,5,7,8,10,12 GetDay=31 case 4,6,9,11 GetDay=30 case else if (Lint_Year mod 400 = 0) or (Lint_Year mod 4 =0 and Lint_Year mod 100<>0) then GetDay=29 else GetDay=28 end if end select end function調用示列 int_A = GetDay(Date)
我也写了一个 num = DateDiff("d", DateSerial(DatePart("yyyy", Date), DatePart("m", Date), 1), DateSerial(DatePart("yyyy", Date), DatePart("m", Date) + 1, 1)) MsgBox "本月天数=" & num
写一个简单的~ num = DateDiff("d", Format(now,"mm-01"), Format(DateAdd("M",1,now),"mm-01")) MsgBox "本月天数=" & num 呵呵,应该可以的~~
if day=30 then
msgbox "30 days"
if day=31
..
end if
if day=28
..
end if
end if
不过,本人出于人道主义事先声明:如果你无聊的时候想去琢磨MonthDay函数是怎么写出来的,要有吐血身亡的心理准备。除了KiteGirl这种一万年不出一个的怪物,没有谁会BT到用这么怪的方式去使用VB的,因为它违背了VB这种语言的初衷,虽然的确比较爽快。Private Sub Form_Load()
For I = 1 To 12
Debug.Print I, MonthDay(I, True)
Next
End SubFunction MonthDay(ByVal pMonth As Byte, Optional pBissextile As Boolean = False, Optional ByVal pDayList As String = "303232332323", Optional ByVal pListPatch As Byte = 20) As Byte
'pMonth 月份
'pBissextile 闰年(如果你查询的月份是闰年的,使它为真。否则可以缺省。)
'pDayList 天数变动表(默认即可)
'pListPatch 递减补偿值(默认即可)
Dim tOutValue As Byte
Dim tBytes() As Byte
tBytes() = pDayList
tBytes(2) = tBytes(2) + (pBissextile And 1)
Dim tBytesIndex As Byte
tBytesIndex = (pMonth - 1) * 2
tOutValue = tBytes(tBytesIndex) - pListPatch
MonthDay = tOutValue
End Function
Day(DateAdd("d", -1, Year(DateAdd("M", 1, Date)) & "-" & Month(DateAdd("M", 1, Date)) & "-1"))方法2:(子程序搞定)function GetDay(Lint_Date as Date) as integer
'Lint_Date為帶年月的日期型數據,也可以自己改成其它的,
'返回日期數,整型
dim Lint_Month as integer
dim Lint_Year as integer Lint_Month=Month(Lint_Date)
Lint_Year=Year(Lint_Date) select case Lint_Month
case 1,3,5,7,8,10,12
GetDay=31
case 4,6,9,11
GetDay=30
case else
if (Lint_Year mod 400 = 0) or (Lint_Year mod 4 =0 and Lint_Year mod 100<>0) then
GetDay=29
else
GetDay=28
end if
end select
end function調用示列 int_A = GetDay(Date)
num = DateDiff("d", DateSerial(DatePart("yyyy", Date), DatePart("m", Date), 1), DateSerial(DatePart("yyyy", Date), DatePart("m", Date) + 1, 1))
MsgBox "本月天数=" & num
num = DateDiff("d", Format(now,"mm-01"), Format(DateAdd("M",1,now),"mm-01"))
MsgBox "本月天数=" & num
呵呵,应该可以的~~