Code Snippet - Find Days In Month
I was bored, so I coded this function to find the amount of days in a month.
VB Code:
Public Function DaysInMonth(Month As Integer, Year As Long) As Integer
Select Case Month%
Case Is = 1
DaysInMonth% = 31
Case Is = 2
If (Year& Mod 4) = 0 Then
DaysInMonth% = 29
Else:
DaysInMonth% = 28
End If
Case Is = 3
DaysInMonth% = 31
Case Is = 4
DaysInMonth% = 30
Case Is = 5
DaysInMonth% = 31
Case Is = 6
DaysInMonth% = 30
Case Is = 7
DaysInMonth% = 31
Case Is = 8
DaysInMonth% = 31
Case Is = 9
DaysInMonth% = 30
Case Is = 10
DaysInMonth% = 31
Case Is = 11
DaysInMonth% = 30
Case Is = 12
DaysInMonth% = 31
Case Else:
DaysInMonth% = 0
End Select
End Function
Enjoy,
Sir Loin
Re: Code Snippet - Find Days In Month
A little less code would do the same trick
VB Code:
Public Function DaysInMonth(Month As Integer, Year As Long) As Integer
Select Case Month
'January, March, May, July, August, October, December
Case 1, 3, 5, 7, 8, 10, 12
DaysInMonth = 31
'Februrary
Case 2
If (Year& Mod 4) = 0 Then
DaysInMonth = 29
Else:
DaysInMonth = 28
End If
'April, June, September, November
Case 4, 6, 9, 11
DaysInMonth = 30
End Select
End Function
Private Sub Command1_Click()
Dim intMonth As Integer
intMonth = DaysInMonth(2, 2000)
MsgBox intMonth
End Sub
Re: Code Snippet - Find Days In Month
. . . and a little less code:
VB Code:
Private Function GetNumberDays(dtDate As Date) As Integer
Dim strDays As String
dtDate = Format(dtDate, "mm/yyyy")
dtDate = DateAdd("m", 1, dtDate)
strDays = Day(dtDate - 1)
GetNumberDays = strDays
End Function
and you would call it as follows:
VB Code:
Private Sub Command1_Click()
MsgBox GetNumberDays("2/23/2004")
End Sub
Note you must submit a vaild date for the month and year to the function.
Re: Code Snippet - Find Days In Month
Even less code and no variables:
VB Code:
Function GetMonthDays (MonthToCheck As Date) As Integer
GetMonthDays = Day(DateAdd("d", -1, DateAdd("m", 1, DateAdd("d", -1 * (Day(MonthToCheck)-1)))))
End Function
Tg
Re: Code Snippet - Find Days In Month
Hardly any code and look mom "No variables":
VB Code:
Function GetMonthDays(MonthToCheck As Date) As Integer
GetMonthDays = Day((DateAdd("m", 1, Format(MonthToCheck, "mm/yyyy"))) - 1)
End Function
and call it like this:
VB Code:
Private Sub Command1_Click()
MsgBox GetMonthDays("02/23/2004")
End Sub