填写下周列表,其中包含过去20周的周数

时间:2014-01-06 11:24:11

标签: asp.net vb.net

我想有一个下拉列表,过去20周(例如)。问题在于,可能会遇到另一年。我写的功能(见下文)确实有效,只要没有进入前一年。 有没有人在过去20周内填写下拉列表或数组,并考虑到文化?

   Private Sub LoadWeeknumbers()
        Dim ciCurr As CultureInfo = CultureInfo.CurrentCulture
        ' fill the combobox with the last 10 weeks
        ddlWeeknumber.Items.Clear()
        'then the 52 voorbije weken
        For i As Integer = 0 To 52
            Dim week As String = ciCurr.Calendar.GetWeekOfYear(DateTime.Now.AddDays(i * -7), CalendarWeekRule.FirstDay, DayOfWeek.Monday)
            Dim weeknr As String = week + "/" & DateTime.Now.AddDays(i * -7).Year.ToString()
            weeknr = weeknr.Replace("54/", "1/") 'for some reason 54 is in this loop, so we replace it

            ddlWeeknumber.Items.Add("Week: " & weeknr.ToString)
        Next
    End Sub

3 个答案:

答案 0 :(得分:1)

也许是这样的:

Dim week = GetIso8601WeekOfYear(DateTime.Now)
Dim year = DateTime.Now.Year
For i As Integer = 0 To 19
    ddlWeeknumber.Items.Add("Week: " + week.ToString() + "/" + year.ToString())
    If week = 1 Then
        week = 52
        year -= 1
    Else
        week -= 1
    End If
Next

Public Shared Function GetIso8601WeekOfYear(time As DateTime) As Integer
    Dim day As DayOfWeek = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time)
    If day >= DayOfWeek.Monday AndAlso day <= DayOfWeek.Wednesday Then
        time = time.AddDays(3)
    End If

    Return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
End Function

答案 1 :(得分:0)

问题出现在一周开始于一周的中间,所以声明

DateTime.Now.AddDays(i * -7)将在上一年的返回并错过今年的1周

我将代码重写为VB.NET,如下所示。这可以解决问题(对不起,我不是VB.NET

的专家
Private Sub LoadWeeknumbers()
    Dim ciCurr As CultureInfo = CultureInfo.CurrentCulture
    ' fill the combobox with the last 10 weeks
    ddlWeeknumber.Items.Clear()
    'then the 52 voorbije weken
    Dim weekOverlapped As Boolean = False

    For i As Integer = 0 To 52
        Dim date as DateTime = DateTime.Now.AddDays(i * -7)

        If date.Year <> DateTime.Now.Year And weekOverlapped <> True  
           date = new DateTime(DateTime.Now.Year, 1, 1)
           weekOverlapped = True
        End IF

        Dim week As String = ciCurr.Calendar.GetWeekOfYear(date , CalendarWeekRule.FirstDay, DayOfWeek.Monday)
        Dim weeknr As String = week + "/" & date.Year.ToString()
        weeknr = weeknr.Replace("54/", "1/") 'for some reason 54 is in this loop, so we replace it

        ddlWeeknumber.Items.Add("Week: " & weeknr.ToString)
    Next
End Sub

答案 2 :(得分:0)

感谢您的帮助!

我使用不同的方法成功创建了所需的列表。下面代码的结果是所需的列表,但没有53/2013项。

Private Sub LoadWeeknumbers()
    Dim currentCulture = CultureInfo.CurrentCulture
    Dim week As Integer = currentCulture.Calendar.GetWeekOfYear(DateTime.Now, currentCulture.DateTimeFormat.CalendarWeekRule.FirstFourDayWeek, currentCulture.DateTimeFormat.FirstDayOfWeek)
    Dim weeksAdded = 0
    For i As Integer = week To 1 Step -1
        '  Dim week As String = ciCurr.Calendar.GetWeekOfYear(DateTime.Now.AddDays(i * -7), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
        Dim weeknr As String = i & "/" & DateTime.Now.Year.ToString()
        weeknr = weeknr.Replace("54/", "1/") 'for some reason 54 is in this loop, so we replace it

        ddlWeeknumber.Items.Add("Week: " & weeknr.ToString)
        weeksAdded += 1
    Next i
    'now add another set of weeknumbers to the dropdownlist
    Dim nextDate = New DateTime(DateTime.Now.Year - 1, 12, 27)
    week = currentCulture.Calendar.GetWeekOfYear(nextDate, currentCulture.DateTimeFormat.CalendarWeekRule.FirstFourDayWeek, currentCulture.DateTimeFormat.FirstDayOfWeek)

    For i As Integer = week To 1 Step -1
        If (weeksAdded < 53) Then
            '  Dim week As String = ciCurr.Calendar.GetWeekOfYear(DateTime.Now.AddDays(i * -7), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
            Dim weeknr As String = i & "/" & nextDate.Year.ToString()
            weeknr = weeknr.Replace("54/", "1/") 'for some reason 54 is in this loop, so we replace it

            ddlWeeknumber.Items.Add("Week: " & weeknr.ToString)
            weeksAdded += 1
        End If
    Next i

End Sub