Click here to Skip to main content
14,735,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I am getting wrong calculation for the count of weeks.

I have two dropdown values.

First values : 31-2011 (Week & Year)
Second Values : 1-2013 (Week & Year)..

When i calculate the duration of these two, i am getting total Weeks as "104700".

Actually i am getting wrong duration for these values.

I need to get "75" weeks only.

Because, 31-2011 will have 52 weeks (for 2011 = 21 Weeks (31week -52week))
2012 = We will have 53 Weeks (so 21+53 Weeks)
2013 = will have 1 Week ( So, 22+53+1) = 75..

What is the problem in my code..? where i need to change.

See my code
Dim parts1 As String() = drpStweek.SelectedItem.ToString().Split("-")
            Dim parts2 As String() = drpEdweek.SelectedItem.ToString().Split("-")
            If Integer.TryParse(parts1(0), week1) AndAlso Integer.TryParse(parts1(1), year1) AndAlso Integer.TryParse(parts2(0), week2) AndAlso Integer.TryParse(parts2(1), year2) Then
                If year1 <= year2 Then
                    If year1 = year2 Then
                        Duration = week2 - week1
                        Duration += (52 - week1) + week2
                        If year2 - year1 > 1 Then
                            Duration += (52 * year2 - year1)
                        End If
                    End If
                End If
End If
Updated 31-Jul-11 22:05pm

1 solution

Rather than trying to fix that - which isn't going to work because a year is not 52 weeks - instead work out the number of days and divide by 7. Then all you have to do is cope with a minor adjustment for the start and end week days, and you are there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900