DateSerial, Day, Month, and Year Function Examples

The following example uses the DateSerial function together with the Year, Month, and Day functions to return the number of days in a month. You can pass either a date or a string to the DaysInMonth function.

Function DaysInMonth(dteInput As Date) As Integer
    Dim intDays As Integer

    ' Add one month, subtract dates to find difference.
    intDays = DateSerial(Year(dteInput), _
        Month(dteInput) + 1, Day(dteInput)) _
        DateSerial(Year(dteInput), _
        Month(dteInput), Day(dteInput))
    DaysInMonth = intDays
    Debug.Print intDays
End Function

The following Sub procedure shows several different ways that you might call the DaysInMonth function.

Sub CallDaysInMonth()
    Dim intDays As Integer
    intDays = DaysInMonth(#4/1/96#)
    intDays = DaysInMonth("4-1-96")
    intDays = DaysInMonth("April 1, 1996")
End Sub