返回一年中正确的周数

时间:2014-11-05 07:17:03

标签: vb.net calendar

previous question中,我向社区请求了如何根据请求的周数显示一周的支持。这个问题现在已经演变成如何在一年中显示正确的周数的问题。

我已经根据从StackExchange网络中的其他线程获得的输入开始使用此方法,但它显示错误的周值。

Protected Friend Shared Function GetNumberOfWeeksInAYear(ByVal GetYear As Nullable(Of Integer)) As Nullable(Of Integer)
            Dim RequestedYear As DateTime = New DateTime(GetYear, 12, 31)

            Return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(RequestedYear, CalendarWeekRule.FirstFourDayWeek, FirstDayOfWeek.System)
        End Function

year参数表示请求的年份(作为整数),返回值也是表示返回周数的整数。此方法用于在自定义日历周视图中设置导航按钮。

现在进行一些测试。 2003年有52周,而2004年有53周。我通过比较各种日历证实了这一点。上面的代码虽然返回2003年的53和2004年的52.我已经尝试在输入中添加一年,但它不会改变输出。

如何改进方法以使其输出正确的周值?

2 个答案:

答案 0 :(得分:1)

    Public Shared Function NumberOfWeeks(dateFrom As DateTime, dateTo As DateTime) As Integer
    Dim Span As TimeSpan = dateTo.Subtract(dateFrom)

    If Span.Days <= 7 Then
        If dateFrom.DayOfWeek > dateTo.DayOfWeek Then
            Return 2
        End If

        Return 1
    End If

    Dim Days As Integer = Span.Days - 7 + CInt(dateFrom.DayOfWeek)
    Dim WeekCount As Integer = 1
    Dim DayCount As Integer = 0

    WeekCount = 1
    While DayCount < Days
        DayCount += 7
        WeekCount += 1
    End While

    Return WeekCount
End Function

答案 1 :(得分:1)

当您将错误的类型传递给Option Strict时,您应该启用Calendar.GetWeekOfYear

GetWeekOfYear(Integer theYear, rule As System.Globalization.CalendarWeekRule, 
       firstDayOfWeek As System.DayOfWeek) 

最后一个参数System.DayOfWeek与VisualBasic NameSpace中的FirstDayOfWeek枚举不同。 VB一个用于VB DateAndTime类型,而不是Net DateTime类型。当您打开Option Strict时,编译器会抱怨类型不匹配。

这使用&#34; sv-SE&#34;文化来获得各种规则的周数:

Dim cult = CultureInfo.CreateSpecificCulture("sv-SE")
Dim cal As Calendar = cult.Calendar

' First Day
Console.WriteLine("{0} - {1}", CalendarWeekRule.FirstDay.ToString,
                  cult.Calendar.GetWeekOfYear(RequestedYear,
                  CalendarWeekRule.FirstDay, 
                  cult.DateTimeFormat.FirstDayOfWeek).ToString)

' first four day
Console.WriteLine("{0} - {1}", CalendarWeekRule.FirstFourDayWeek.ToString,
                  cult.Calendar.GetWeekOfYear(RequestedYear,
                  CalendarWeekRule.FirstFourDayWeek,
                  cult.DateTimeFormat.FirstDayOfWeek).ToString)

' first full week
Console.WriteLine("{0} - {1}", CalendarWeekRule.FirstFullWeek.ToString,
                  cult.Calendar.GetWeekOfYear(RequestedYear,
                  CalendarWeekRule.FirstFullWeek, 
                  cult.DateTimeFormat.FirstDayOfWeek).ToString)

' WRONG - requires Option Strict OFF - VB enum
Console.WriteLine("{0} - {1}", "VB FirstDayOfWeek Enum",
                  cult.Calendar.GetWeekOfYear(RequestedYear,
                  CalendarWeekRule.FirstFullWeek, FirstDayOfWeek.System).ToString)   

输出:

FirstDay - 53
FirstFourDayWeek - 53
FirstFullWeek - 52
VB FirstDayOfWeek Enum - 52

您可能想要使用它的方式:

    ' just to make things shorter
    Dim cult = CultureInfo.CurrentCulture

    Return cult.Calendar.GetWeekOfYear(RequestedYear,
                                        CalendarWeekRule.FirstDay,
                                        cult.DateTimeFormat.FirstDayOfWeek)

最终结果是,您迫使日历始终使用星期日作为DateTimeFormat sv-SE文化{{1}}中定义的第一周的第一天而不是星期一。