Legolas786
asked on
Display 2nd monday of the month
Hi
I use the following code to display the monday date of the current week
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
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)
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
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)
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
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nope, i only added the line at the end which shows the date in the text box
My code in total
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
You need to adjust the GetDate function to pull out the SecondWeek (not the LastWeek, as in the first example)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry all, this was my fault as my program was not compiling correct and was running an older version, both worked thanks again
ASKER
Thanks guys
Open in new window