# QuestionGetting days dates from WeekOfYear number

#### nicorvp

##### Member
Hi, I need to get the first day (datetime) of the week from the weekofyear number. Now im doing a loop through the days of the year but I'm looking for a smarter solution.
Thanks
Nico

#### r3plica

##### Well-known member
I am assuming you mean the current year.
Try:

VB.NET:
``````    Function ReturnDateForWeekNumber(ByVal iWeek As Integer) As DateTime
Return DateAdd(DateInterval.WeekOfYear, iWeek - 1, FirstDayOfYear)
End Function

Function FirstDayOfYear() As DateTime
Return New DateTime(Year(Now), 1, 1)
End Function``````

#### JohnH

##### VB.NET Forum Moderator
Staff member
I am assuming you mean the current year.
Try:

VB.NET:
``````    Function ReturnDateForWeekNumber(ByVal iWeek As Integer) As DateTime
Return DateAdd(DateInterval.WeekOfYear, iWeek - 1, FirstDayOfYear)
End Function

Function FirstDayOfYear() As DateTime
Return New DateTime(Year(Now), 1, 1)
End Function``````
Where is Friday June 11th the first day of week 24 ?

#### nicorvp

##### Member
I've found a way
VB.NET:
``````  Public Shared Function GetFirstDayOfWeek(ByVal week As Integer, ByVal year As Integer) As DateTime
Dim ciCurr As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
Dim newYearDay As DateTime = New DateTime(year, 1, 1)
Dim firstWeekNum As Integer = ciCurr.Calendar.GetWeekOfYear(newYearDay, Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
Dim firstWeekDay As DateTime = GetFirstDayOfWeek(newYearDay)
If firstWeekNum = 1 Then Week -= 1
Return DateAdd(DateInterval.WeekOfYear, Week, firstWeekDay)
End Function
Public Shared Function GetLastDayOfWeek(ByVal week As Integer, ByVal year As Integer) As DateTime
Dim ciCurr As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
Dim newYearDay As DateTime = New DateTime(year, 1, 1)
Dim firstweekNum As Integer = ciCurr.Calendar.GetWeekOfYear(newYearDay, Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
Dim lastWeekDay As DateTime = GetLastDayOfWeek(newYearDay)
If firstweekNum = 1 Then Week -= 1
Return DateAdd(DateInterval.WeekOfYear, Week, lastWeekDay)
End Function

Public Shared Function GetFirstDayOfWeek(ByVal dt As DateTime) As DateTime
If dt.DayOfWeek = DayOfWeek.Sunday Then
Return dt.AddDays(-6)
End If
Return dt.AddDays(1 - CInt(dt.DayOfWeek))
End Function
Public Shared Function GetLastDayOfWeek(ByVal dt As DateTime) As DateTime
If dt.DayOfWeek = DayOfWeek.Sunday Then
Return dt.AddDays(0)
End If
Return dt.AddDays(7 - CInt(dt.DayOfWeek))
End Function``````