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
Legolas786Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
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

0
Legolas786Author 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
0
AndyAinscowFreelance programmer / ConsultantCommented:
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

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Legolas786Author Commented:
Thanks Andy, but when I tried that it shows the date as 21/11/2017 when it should be 13/11/2017?
0
AndyAinscowFreelance programmer / ConsultantCommented:
It worked perfectly here.  Have you made any adjustments to the code?
0
AndyAinscowFreelance programmer / ConsultantCommented:
SNAG-0045.bmp
0
Legolas786Author 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

0
sirbountyCommented:
You need to adjust the GetDate function to pull out the SecondWeek (not the LastWeek, as in the first example)
0
sirbountyCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0
Legolas786Author Commented:
Sorry all, this was my fault as my program was not compiling correct and was running an older version, both worked thanks again
0
Legolas786Author Commented:
Thanks guys
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.