First Monday of the week

  • Thread starter Thread starter sweetpotatop
  • Start date Start date
S

sweetpotatop

Hi,

I wonder if there is any function in VB or SQL that will return the
date of first Monday of the Monday.

Besides, is there are a way finding the numbers of Monday in a month?

Thanks in advance. Your help is greatly appreciated.
 
Hi,

I wonder if there is any function in VB or SQL that will return the
date of first Monday of the Monday.

The first Monday of Monday? I assume you mean the first Monday of the month
:D I am pretty sure there is, but I don't remember..maybe it is SQL that I
remember having such a method....
Besides, is there are a way finding the numbers of Monday in a month?
The following should work for ya.


Private Function GetDayOfWeekCountForMonth( _
ByVal Month As Integer, _
ByVal Year As Integer, _
ByVal DayOfWeek As DayOfWeek _
) As Integer
Dim count As Integer = 0
Dim d As Date = New Date(Year, Month, 1)

While d.Month = Month AndAlso d.Year = Year
If d.DayOfWeek = DayOfWeek
count += 1
d = d.AddDays(7)
Else
d = d.AddDays(1)
End If
End While

Return count
End Function
 
The first monday of the week? As far as I know, there are rarely more
than one monday in a week... ;)

Hi,

I wonder if there is any function in VB or SQL that will return the
date of first Monday of the Monday.

Neither does mondays often contain more than one monday... ;)

There is no built in function to return the first monday of a month. You
can get the weekday of the first day of the month, and use that to
determine how many days to add to get to the monday.

Something like:

Dim first as DateTime = New DateTime(year, month, 1)
Dim day as Integer = Cast(first.DayOfWeek, Integer)
If day <= 1 Then day = day + 7
Dim firstMonday as DateTime = first.AddDays(8 - day)
Besides, is there are a way finding the numbers of Monday in a month?

One way could be to just repeatedly add seven days to the date until it
reaches the next month.
 
Back
Top