vb.net周转换?

时间:2011-06-29 16:25:25

标签: vb.net date

我正在开发一个ASPX VB.NET文件。我的任务是将表示一年中一周的整数转换为该结束日期。例如,如果用户选择2011年的第4周,我想获得日期= 1/22/11。我如何在VB.NET中执行此操作?

2 个答案:

答案 0 :(得分:2)

不久前我问过a similar question。我还有an answer from J.Skeet的RegularExpressionValidator(如果你需要的话)。

以下是我必须从一周中的某一周获得的日期:

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

这里是RegularExpressionValidator,虽然我的格式(07w42表示2007年的42.week)与你的格式略有不同。

<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>

答案 1 :(得分:0)

您可以使用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