Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleCount the number of Mondays in a month
DescriptionThis example shows how to count the number of Mondays in a month in Visual Basic 6. It uses the WeekDay, DateAdd, and Month functions.
KeywordsMonday, month, count Mondays
CategoriesMiscellany, Software Engineering
 
Get the first date of the month and then use the WeekDay function to see what day of the week that date is. Use DateAdd to add the number of days needed to get to the first Monday of the month. Add weeks to the date until the date is no longer in the month, counting the number of weeks added.
 
' Return the number of Mondays in this month.
Private Function NumMondays(ByVal month_name As String, _
    ByVal year_name As String) As Integer
Dim i As Integer
Dim num_mondays As Integer
Dim test_date As Date
Dim orig_month As Integer

    ' Get the first day of the month.
    test_date = CDate(month_name & " 1, " & year_name)

    ' Get the first Monday.
    Select Case Weekday(test_date)
        Case vbMonday
            ' Leave the date unchanged.
        Case vbTuesday
            test_date = DateAdd("d", 6, test_date)
        Case vbWednesday
            test_date = DateAdd("d", 5, test_date)
        Case vbThursday
            test_date = DateAdd("d", 4, test_date)
        Case vbFriday
            test_date = DateAdd("d", 3, test_date)
        Case vbSaturday
            test_date = DateAdd("d", 2, test_date)
        Case vbSunday
            test_date = DateAdd("d", 1, test_date)
    End Select

    ' Count the Mondays.
    orig_month = Month(test_date)
    Do
        num_mondays = num_mondays + 1
        test_date = DateAdd("ww", 1, test_date)
    Loop While (Month(test_date) = orig_month)

    NumMondays = num_mondays
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated