We help IT Professionals succeed at work.

Display 2nd monday of the month

Hi

I use the following code to display the monday date of the current week

Dim monday As DateTime = Today.AddDays((Today.DayOfWeek - DayOfWeek.Monday) * -1)

Open in new window

I was hoping to amend it so it would display the date of the 2nd monday of the month?

So if I was to run it now it would display 13/11/2017
in december it would display 11/12/2017
in january it would display 08/01/2017

and so on

There is no DayofMonth option whuch i thought would work, any help is appreciated thanks
Comment
Watch Question

Top Expert 2007

Commented:
Found this which should help you...
Public Function GetDate()

    Dim dt As Date = Today

    Dim FirstWeek As Integer = 1
    Dim SecondWeek As Integer = 2
    Dim ThirdWeek As Integer = 3
    Dim FourthWeek As Integer = 4
    Dim LastWeek As Integer = 5

    MsgBox(GetNthDayOfNthWeek(dt, DayOfWeek.Monday, LastWeek).ToString)

End Function

Public Function GetNthDayOfNthWeek(ByVal dt As Date, ByVal DayofWeek As Integer, ByVal WhichWeek As Integer) As Date
    'specify which day of which week of a month and this function will get the date
    'this function uses the month and year of the date provided

    'get first day of the given date
    Dim dtFirst As Date = DateSerial(dt.Year, dt.Month, 1)

    'get first DayOfWeek of the month
    Dim dtRet As Date = dtFirst.AddDays(6 - dtFirst.AddDays(-(DayofWeek + 1)).DayOfWeek)

    'get which week
    dtRet = dtRet.AddDays((WhichWeek - 1) * 7)

    'if day is past end of month then adjust backwards a week
    If dtRet >= dtFirst.AddMonths(1) Then
        dtRet = dtRet.AddDays(-7)
    End If

    'return
    Return dtRet

End Function
'ref: https://stackoverflow.com/questions/17520008/how-do-i-get-first-second-or-last-tuesday-or-any-day-of-the-week-of-a-given-m

Open in new window

Author

Commented:
Thanks, I already saw that but its not what I require, when i ran it it just displayed the last weekday of the month
AndyAinscowFreelance programmer / Consultant

Commented:
Call it like this with the month and year you want
        Dim monday As DateTime = SecondMonday(12, 2017)

 
   Private Function SecondMonday(mnth As Integer, yr As Integer) As DateTime
        Dim dte As DateTime = New DateTime(yr, mnth, 8, 0, 0, 0)
        While dte.DayOfWeek <> DayOfWeek.Monday
            dte = dte.AddDays(1)
        End While
        SecondMonday = dte
    End Function

Open in new window

Author

Commented:
Thanks Andy, but when I tried that it shows the date as 21/11/2017 when it should be 13/11/2017?
AndyAinscowFreelance programmer / Consultant

Commented:
It worked perfectly here.  Have you made any adjustments to the code?
AndyAinscowFreelance programmer / Consultant
Commented:
SNAG-0045.bmp

Author

Commented:
Nope, i only added the line at the end which shows  the date in the text box

My code in total

   
 Private Sub Button16_Click(sender As System.Object, e As System.EventArgs) Handles Button16.Click

        Dim monday As DateTime = SecondMonday(12, 2017)
        TextBox1.Text = (monday)

    End Sub


    Private Function SecondMonday(mnth As Integer, yr As Integer) As DateTime
        Dim dte As DateTime = New DateTime(yr, mnth, 8, 0, 0, 0)
        While dte.DayOfWeek <> DayOfWeek.Monday
            dte = dte.AddDays(1)
        End While
        SecondMonday = dte
    End Function

Open in new window

Top Expert 2007

Commented:
You need to adjust the GetDate function to pull out the SecondWeek (not the LastWeek, as in the first example)
Top Expert 2007
Commented:
To simplify it, try this - that code should get you what you need:
Public Function GetDate()
    Dim dt As Date = Today
    'removed First, Third, Fourth & Last since you don't need those
    Dim SecondWeek As Integer = 2
    MessageBox.Show(GetSecondMonday(dt, DayOfWeek.Monday, SecondWeek).ToString)
End Function

Public Function GetSecondMonday(ByVal dt As Date, ByVal DayofWeek As Integer, ByVal WhichWeek As Integer) As Date
    Dim dtFirst As Date = DateSerial(dt.Year, dt.Month, 1)
    Dim dtRet As Date = dtFirst.AddDays(6 - (dtFirst.AddDays(-(DayofWeek + 1)).DayOfWeek))
    dtRet = dtRet.AddDays((WhichWeek - 1) * 7)
    If dtRet >= dtFirst.AddMonths(1) Then dtRet = dtRet.AddDays(-7)
    Return dtRet
End Function

Open in new window

AndyAinscowFreelance programmer / Consultant

Commented:
Ahh, I think you misread your message box

SNAG-0046.bmp
I'd tested it with more than the current month and my code snippet was for December.

Author

Commented:
Sorry all, this was my fault as my program was not compiling correct and was running an older version, both worked thanks again

Author

Commented:
Thanks guys