How to get the first Monday of the month

Hi

How do I get the first Monday of the month?
If the current month is March, 2023, the first Monday should be 2/27/2023.
If the current month is April 2023, the first Monday should be 3/27/2023.

I can get the first week number of the month from a date, and from there tried to get the first Monday of the month but cannot quite get the correct date.

Here’s what I tried so far.

Get the first week number of the month:

firstWeeknum = cult.Calendar.GetWeekOfYear(CDate(str_firstDate), CalendarWeekRule.FirstDay, DayofWeek.Monday).ToString

Get the first date of the year:

dateJan1 = New dateTime(2023,1,1)

Get the first Monday of the month:

dateJan1.AddDays(7*(Convert.ToInt32(firstWeeknum)-1)-CInt(dateJan1.DayOfWeek)+1).ToString

Thank you

Reference:

DayOfWeek is 0, not 7, for Sunday. And this year starts with a Sunday.

Test to change the CInt() part to:

CInt(If(dateJan1.DayOfWeek=0, 7, dateJan1.DayOfWeek))
1 Like

Thank you for your answer.

I got 2/26/2023 from your code.
I need to get the first Monday of the month which is 2/27/2023 for the current month.

How do I get that?

Did you forget the +1?

Yes that’s it.

By the way, why + 1 or -1 is needed?

and why do you put 7 in CInt(If(dateJan1.DayOfWeek=0, 7, dateJan1.DayOfWeek)) ??

Because the code requires Sunday to be day 7. Unfortunately, the enum type DayOfWeek defines Sunday as 0.

If you start from a Sunday, subtracting 7 days will give you Sunday in the previous week. Then adding back one day (+1) gives you Monday.

1 Like

Thank you for your explanation.

I got a better understanding. I am going to learn about it.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.