-
Aug 12th, 2005, 07:36 PM
#1
Thread Starter
Fanatic Member
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
-
Aug 15th, 2005, 05:36 AM
#2
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
Last edited by Hack; Aug 15th, 2005 at 05:40 AM.
-
Aug 17th, 2005, 01:09 PM
#3
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.
Last edited by Mark Gambo; Aug 17th, 2005 at 05:30 PM.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Aug 17th, 2005, 03:25 PM
#4
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
-
Aug 17th, 2005, 05:27 PM
#5
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
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|