Pacific Database

Home | Contact | FAQs | View Cart

A world of information at your fingertips

Date & Time :: Month functions

On this page you will find several functions for calculating month-related dates.

Month number

    myInt = Month(dte)
    myInt = DatePart("m", dte)

Month name

    myString = MonthName(Month(dte), False) 'Not abbreviated
    myString = MonthName(Month(dte), True) 'Abbreviated
    myString = Format(dte, "mmm")

Days in a month

    myInt = Day(DateSerial(Year(dte), Month(dte) + 1, 0))

First day of a specified month

    myDate = DateSerial(Year(dte), Month(dte), 1) 

Last day of a specified month

    myDate = DateSerial(Year(dte), Month(dte) + 1, 0)

First day of next month

    myDate = DateSerial(Year(dte), Month(dte) + 1, 1)

Last day of next month

    myDate = DateSerial(Year(dte), Month(dte) + 2, 0)

First day of previous month

    myDate = DateSerial(Year(dte), Month(dte) - 1, 1)

Last day of previous month

    myDate = DateSerial(Year(dte), Month(dte), 0)

First Friday in a specified month

     myDate = DateSerial(Year(dte), Month(dte), 0) + _
            (8 - WeekDay(DateSerial(Year(dte), Month(dte), 0), vbFriday))

Last Friday in a specified month

     myDate = DateSerial(Year(dte), Month(dte) + 1, 0) - _
               (WeekDay(DateSerial(Year(dte), Month(dte)+1,0), vbFriday) - 1)

Number of Occurrences of a specific day in a specific month

    Public Function OccurrencesOfDayInMonth(dte As Date, iDay As VbDayOfWeek) As Integer
        'Calculates the number of times the specified
        'day of the week occurs within the specified
        'month.
        '
        'dte is specified as a date because February
        'has an extra day in leap years.
        '
        'INPUTS:
        '  dte = a date in the month/year in question.
        '  iDay = the day of the week you to count.
        '
        'RETURN VALUE:
        '  Integer value.
        '
        Dim dteFirst As Date
        Dim dteLast As Date
        
        'Get the date of the first specified day in the month.
        dteFirst = DateSerial(Year(dte), Month(dte), 0) + _
            (8 - Weekday(DateSerial(Year(dte), Month(dte), 0), iDay))
        
        'Get the date of the last specified day in the month.
        dteLast = DateSerial(Year(dte), Month(dte) + 1, 0) - _
            (Weekday(DateSerial(Year(dte), Month(dte) + 1, 0), iDay) - 1)
        
        'The rest is easy.
        OccurrencesOfDayInMonth = (Day(dteLast) - Day(dteFirst)) / 7 + 1
    End Function