1

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?

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
user371819
  • 95
  • 1
  • 4
  • 18
  • 1
    Is this homework? If so, you really shouldn't ask here. We can help you in pointing out flaws in logic/syntax -- but it's not very honest on either parties part to do your assignment. Perhaps you have some form of what you've tried (that doesn't work), which we could look at. ;) – George Johnston Jun 29 '11 at 16:30
  • 1
    Look at this http://stackoverflow.com/questions/659183/how-do-i-get-the-month-number-from-the-year-and-week-number-in-c/659451#659451 – Yet Another Geek Jun 29 '11 at 16:31
  • Dim d = new DateTime(2011, 1, 1).AddDays((4 - 1) * 7) – Magnus Jun 29 '11 at 16:42
  • No, this is not homework. I am new to VB.NET though. – user371819 Jun 29 '11 at 16:49
  • @Magnus, when I use your code above, I get the error: "'DateTime' is a type and cannot be used as an expression." – user371819 Jun 29 '11 at 16:52
  • Thanks guys. I figured this out now! Here's what I did: Dim FirstWeekDate As Date FirstWeekDate = DateSerial(2011, 1, 1).AddDays((dEndWeek.SelectedValue - 1) * 7) – user371819 Jun 29 '11 at 17:01

2 Answers2

2

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>
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Short note: the _GetWeekOfYear_ method of of the _Calendar_ class is not [ISO8601](http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/date_and_time_format.htm) conform. See also [MSDN](http://msdn.microsoft.com/en-us/library/system.globalization.gregoriancalendar.getweekofyear.aspx). –  Jun 30 '11 at 08:57
  • Hi Tim, if is this is not a ISO8601 is it still applicable or valid to use as a reference ? – GoroundoVipa Jul 14 '14 at 09:09
0

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