Public Function DaysInMonth(ByVal Year As UShort, ByVal Month As Byte) As Byte ' Function that returns the number of days in the given month ' using the current Gregorian calendar ' Example: DaysInMonth(2000, 2) = 29 ' ' Visustin sample algorithm ' ©2006 Aivosto Oy (www.aivosto.com) Select Case Month Case 1, 3, 5, 7, 8, 10, 12 ' Jan, Mar, May, Jul, Aug, Oct, Dec have 31 days Return 31 Case 4, 6, 9, 11 ' Apr, Jun, Sep, Nov have 30 days Return 30 Case 2 ' February - need to check for leap year If (Year Mod 400) = 0 Then ' Year divisible by 400: Leap year Return 29 ElseIf (Year Mod 100) = 0 Then ' Year divisible by 100 (but not 400): Not leap year Return 28 ElseIf (Year Mod 4) = 0 Then ' Year divisible by 4: Leap year Return 29 Else ' Not leap year Return 28 End If Case Else ' Error, invalid month Return 0 End Select End Function
Hide code
Visustin flow chart for VB.NET