Link to home
Start Free TrialLog in
Avatar of Legolas786
Legolas786

asked on

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
Avatar of sirbounty
sirbounty
Flag of United States of America image

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

Avatar of Legolas786
Legolas786

ASKER

Thanks, I already saw that but its not what I require, when i ran it it just displayed the last weekday of the month
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

Thanks Andy, but when I tried that it shows the date as 21/11/2017 when it should be 13/11/2017?
It worked perfectly here.  Have you made any adjustments to the code?
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

You need to adjust the GetDate function to pull out the SecondWeek (not the LastWeek, as in the first example)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh, I think you misread your message box

User generated image
I'd tested it with more than the current month and my code snippet was for December.
Sorry all, this was my fault as my program was not compiling correct and was running an older version, both worked thanks again
Thanks guys