FirstDayOfWeek

  • Thread starter Thread starter Thammarat charoenchai.
  • Start date Start date
T

Thammarat charoenchai.

Hi, I'm wonder VB have som function to get First day of the week?

like I'm have

dim d1 as datetime=12/5/2007

d2 = firstweekday(d1) '=7/5/2007

thank you very much
 
Here's the solution for you:

Private Function GetPreviousMonday(ByVal dt As DateTime) As String
Dim intWeekday As DayOfWeek = dt.DayOfWeek
Dim intSubtract As Integer = 0
Select Case intWeekday
Case 0 ' Sunday
intSubtract = -6
Case 1 ' Monday
intSubtract = 0
Case 2 ' Tuesday
intSubtract = -1
Case 3 ' Wednesday
intSubtract = -2
Case 4 ' Thursday
intSubtract = -3
Case 5 ' Friday
intSubtract = -4
Case 6 ' Saturday
intSubtract = -5
Case Else
Return "Unable to get day of the week"
End Select
Return dt.AddDays(intSubtract).ToString("MM/dd/yyyy")
End Function

Usage:

MessageBox.Show(GetPreviousMonday(#5/4/2007#))

I hope this helps,
 
Shorter version, same as N. Coders, but less code.

Private Function GetStartOfWeek(selectedDate as datetime) as DateTime
Select Case SelectedDate.DayOfWeek
Case DayOfWeek.Sunday
return selecteddate.adddays(-6)
Case Else
Return selectedDate.AddDays(1 - selectedDate.DayOfWeek)
End Function
 
Thanks you very much. That's mean vb not have build in function for this .

i already coding.

Thanks again. :D
 
Back
Top