Results 1 to 7 of 7

Thread: [RESOLVED] Find All Mondays In A Month

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Resolved [RESOLVED] Find All Mondays In A Month

    How would I determine what the dates are for all Mondays for any given month, for any given year?

    For example: If I put 6/2011 in a textbox, what would I need to do in order for my program to tell me that for June of 2011, the Mondays are:

    6
    13
    20
    27
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Find All Mondays In A Month

    Code:
    Dim intDay As Integer
    Dim d As Date
    
    d = Text1.Text
        
    Do
        intDay = intDay + 1
        If Format(d + intDay, "ddd") = "Mon" Then
            Debug.Print Format(d + intDay, "d")
        End If
    Loop Until Format(d + intDay, "mmm") <> Format(d + intDay + 1, "mmm")

  3. #3
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Find All Mondays In A Month

    try this
    Code:
    Private Sub Command1_Click()
    Dim EndDay As Integer, dt As Date
    dt = #6/1/2011#
    EndDay = LastDay(2011, 6)
    
    For i = 1 To EndDay
        If Weekday(dt) = 2 Then '2 = monday
            Debug.Print Day(dt)
        End If
        dt = dt + 1
    Next
    End Sub
    
    Function LastDay( _
        testYear As Integer, _
        testMonth As Integer _
        ) As Integer
    
        LastDay = Day(DateSerial(testYear, testMonth + 1, 0))
    
    End Function
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Find All Mondays In A Month

    Both solutions could work. The solution in Post#2 isn't locale aware if that could be a problem.

    Optimization point: In either solution, wouldn't you just want to find the 1st Monday, then add 7 to that day's value until you exceed the last day of the month? Loop-wise for a 4 monday month, a max of 10 iterations, otherwise you'll loop the number days of the month.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,451

    Re: Find All Mondays In A Month

    This works without too much looping and no string tests:

    Code:
    Sub FindMondays(ByVal p_Month As Long, ByVal p_Year As Long)
       Dim l_TestDay As Long
       
       ' Find first Monday of month
       l_TestDay = 8 - Weekday(DateSerial(p_Year, p_Month, 1), vbTuesday)
       
       Do
          ' Output found Monday in the month
          Debug.Print "Monday in " & MonthName(p_Month) & " " & p_Year & ": " & l_TestDay
          
          l_TestDay = l_TestDay + 7  ' Add a week to previously determined Monday
          
          ' Loop until dateserial overflows into next month
       Loop While Month(DateSerial(p_Year, p_Month, l_TestDay)) = p_Month
    End Sub

  6. #6

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Find All Mondays In A Month

    Thanks to all for the code and the responses.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  7. #7
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: [RESOLVED] Find All Mondays In A Month

    if ur problm solved pls mark thread as resolved.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width