Reputation: 105
I am developing an ASPX VB.NET file. My assignment is to convert an integer representing week of the year into that end date. For example, if user selects Week 4 for 2011, I want to get date = 1/22/11. How do I do this in VB.NET?
Upvotes: 1
Views: 2373
Reputation: 460108
I've asked a similar question a short while ago. I also have an answer from J.Skeet for a RegularExpressionValidator(if you need one).
Here is what i have to get a date from a week of year:
Public Shared Function FirstDateOfWeek(ByVal year As Integer, ByVal weekOfYear As Integer) As DateTime
Dim jan1 As New DateTime(year, 1, 1)
Dim daysOffset As Integer = CInt(Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) - CInt(jan1.DayOfWeek)
Dim firstWeekDay As DateTime = jan1.AddDays(daysOffset)
Dim curCulture As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CurrentCulture
Dim firstWeek As Integer = curCulture.Calendar.GetWeekOfYear(jan1, curCulture.DateTimeFormat.CalendarWeekRule, curCulture.DateTimeFormat.FirstDayOfWeek)
If firstWeek <= 1 Then
weekOfYear -= 1
End If
Return firstWeekDay.AddDays(weekOfYear * 7)
End Function
and here is the RegularExpressionValidator, although my format(07w42
means 42.week in year 2007) differs a little from yours.
<asp:RegularExpressionValidator ID="CalWeekFormat" runat="server"
ControlToValidate="TxtCalWeek" Display="None" EnableClientScript="true"
ErrorMessage="Enter valid Year/Calendarweek-Format: examplary format '09w23' or '9w23' for year 2009 and week 23"
style="visibility:hidden"
ValidationExpression="^\d{1,4}[wW](\d|[0-4]\d|5[0123])$"
ValidationGroup="VG_SAVE">*</asp:RegularExpressionValidator>
Upvotes: 2
Reputation:
You can use the class Week of the Time Period Library for .NET:
Imports Itenso.TimePeriod
Module GetStartOfWeekDemo
Sub Main()
Console.WriteLine("Week start " & GetStartOfWeek(2011, 4))
Console.ReadKey()
End Sub
Public Function GetStartOfWeek(ByVal year As Integer, ByVal weekOfYear As Integer) As DateTime
Dim week As Week = New Week(year, weekOfYear)
Return week.Start
End Function
End Module
Upvotes: 0