none
How to get the date of the first Monday of a month? RRS feed

  • Question

  • Hi 

    How to get the date of the first Monday of a month?

    Please provide an algorithm,thanks.

    Monday, August 6, 2018 3:53 AM

Answers

  • Check this algorithm and example too:

    Dim month = 8
    Dim year = 2018
    
    Dim result = New DateTime(year, month, 1)
    Do While result.DayOfWeek <> DayOfWeek.Monday
       result = result.AddDays(1)
    Loop
    
    Console.WriteLine(result)


    • Edited by Viorel_MVP Monday, August 6, 2018 5:45 AM
    • Marked as answer by CLG123 Monday, August 6, 2018 11:51 PM
    Monday, August 6, 2018 5:44 AM
  • Hi 

    I wrote a method, I hope it can help you.

     Private Function jihao(ByVal nian As Integer, ByVal yue As Integer, ByVal index1 As Integer, ByVal index2 As Integer) As Integer
            Dim y As Integer = nian Mod 400
            Dim day As Integer = y
            Dim t As Integer
    
            For t = 1 To y - 1
                If t Mod 4 = 0 AndAlso t Mod 100 <> 0 Then day += 1
            Next
            Dim newday As Integer = day Mod 7
            Dim c As Integer() = New Integer() {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
            If nian Mod 400 = 0 OrElse (nian Mod 4 = 0 AndAlso nian Mod 100 <> 0) Then c(1) = 29
    
            For s As Integer = 1 To yue - 1
                day += c(s - 1)
            Next
    
            day = day Mod 7
            Dim i As Integer = 0
    
            While True
                i += 1
                If (day + i) Mod 7 = 1 Then
    
                    If day = 0 Then
                        day += 7
                    End If
                    If day > index2 Then
                        Return i + 7 * (index1 - 1) + index2
                    Else
                        Return i + 7 * (index1 - 2) + index2
                    End If
                End If
            End While
        End Function
    

    For example, the second Wednesday of January 2008:

    jihao(2018,1,2,3)

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CLG123 Monday, August 6, 2018 11:50 PM
    Monday, August 6, 2018 5:05 AM

All replies

  • Hi 

    I wrote a method, I hope it can help you.

     Private Function jihao(ByVal nian As Integer, ByVal yue As Integer, ByVal index1 As Integer, ByVal index2 As Integer) As Integer
            Dim y As Integer = nian Mod 400
            Dim day As Integer = y
            Dim t As Integer
    
            For t = 1 To y - 1
                If t Mod 4 = 0 AndAlso t Mod 100 <> 0 Then day += 1
            Next
            Dim newday As Integer = day Mod 7
            Dim c As Integer() = New Integer() {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
            If nian Mod 400 = 0 OrElse (nian Mod 4 = 0 AndAlso nian Mod 100 <> 0) Then c(1) = 29
    
            For s As Integer = 1 To yue - 1
                day += c(s - 1)
            Next
    
            day = day Mod 7
            Dim i As Integer = 0
    
            While True
                i += 1
                If (day + i) Mod 7 = 1 Then
    
                    If day = 0 Then
                        day += 7
                    End If
                    If day > index2 Then
                        Return i + 7 * (index1 - 1) + index2
                    Else
                        Return i + 7 * (index1 - 2) + index2
                    End If
                End If
            End While
        End Function
    

    For example, the second Wednesday of January 2008:

    jihao(2018,1,2,3)

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CLG123 Monday, August 6, 2018 11:50 PM
    Monday, August 6, 2018 5:05 AM
  • Check this algorithm and example too:

    Dim month = 8
    Dim year = 2018
    
    Dim result = New DateTime(year, month, 1)
    Do While result.DayOfWeek <> DayOfWeek.Monday
       result = result.AddDays(1)
    Loop
    
    Console.WriteLine(result)


    • Edited by Viorel_MVP Monday, August 6, 2018 5:45 AM
    • Marked as answer by CLG123 Monday, August 6, 2018 11:51 PM
    Monday, August 6, 2018 5:44 AM