Days in month


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'**********************************************************************************************************
' Name:                 daysInMonth
' Author:               mielk | 2012-03-11
'
' Comment:              Function to return the number of days in a specified month.
'
' Parameters:
'     month             A month for which the number of days is to be returned.
'                       If the month number is not within the range 1-12, a year counter is decremented
'                       or incremented while the month rolls over from 1 to 12 or from 12 to 1.
'     year              Optional parameter.
'                       Year part of a date for which the number of days is to be returned.
'                       If this parameter is not given, the current year is used.
'
'
' Returns:
'     Byte              The number of days in a specified month and year.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2012-03-11       mielk       Function created.
'**********************************************************************************************************
Public Function daysInMonth(month As Integer, Optional Year As Integer) As Byte
    Const METHOD_NAME As String = "daysInMonth"
    '------------------------------------------------------------------------------------------------------
    Dim iYear As Integer
    '------------------------------------------------------------------------------------------------------

    'Variable [iYear] assumes the value of a given parameter year or the current year if ----------------|
    'this parameter is omitted.                                                                         '|
    If Year = 0 Then                                                                                    '|
        iYear = VBA.Year(VBA.Date)                                                                      '|
    Else                                                                                                '|
        iYear = Year                                                                                    '|
    End If                                                                                              '|
    '----------------------------------------------------------------------------------------------------|

    daysInMonth = VBA.Day(VBA.DateSerial(iYear, month + 1, 1) - 1)

End Function