15,967,252 members
See more:
Hi,

I want to get the All Week numbers between two years. Now I am getting only the count. But, I want to get all the list of week numbers between two years instead of total count.

The code is below:

```Dim dateFrom As New DateTime(2006, 7, 1)
Dim dateTo As New DateTime(2011, 12, 31)
' Difference in days, hours, and minutes.
'NumberOfWeeks(oldDate, newDate)
Dim Span As TimeSpan = dateTo.Subtract(dateFrom)
If Span.Days <= 7 Then
If dateFrom.DayOfWeek > dateTo.DayOfWeek Then
End If

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

Regards,
Ganesh.S
Posted
Updated 25-Jun-11 15:58pm
v4
Manfred Rudolf Bihy 25-Jun-11 13:05pm
The way you're doing the week number calculation is wrong. Please see my elaborated example below. It uses a helper class called Week to find all weeks between two dates and can be extensively configured to abide to the rules of your locale.

## Solution 1

You can use Calendar.Getweekofyear.[^] to find the work week of the from date and use the same to get the workweek of to date.

then create the list with from and to values, simple loop would do.

Manfred Rudolf Bihy 25-Jun-11 13:06pm
Yes that is the method to use! 5+

Please also see my elaborated example below.
Espen Harlinn 25-Jun-11 13:20pm
My 5
thatraja 25-Jun-11 13:27pm
5!

## Solution 3

The way you're doing the week number caculation is wrong as Vivek[^] already pointed out in his answer the correct way to do this is to use `Calendar.GetWeekOfYear`. In my example below I used the `GregorianCalendar` with `GregorianCalendarType.Localized`. For the method `GetWeekOfYear` to work correctly though you will have to use `CalendarWeekRule` to specify how a week is started when transitioning from one year to the next. Equally important is how the first `DayOfWeek` is defined in your locale. In my country Monday is the correct day, but that may differ where you are.
Depending on the value of `CalendarWeekRule` there are different results to be obtained. Weeks may either span across a year boundary or my have less than 7 days. The following list exlains how this value can be used to define the behavior:

1. FirstDay: Indicates that the first week of the year starts on the first day of the year and ends before the following designated first day of the week. The value is 0. This value ensures that a week does not span two years.
2. FirstFullWeek: Indicates that the first week of the year begins on the first occurrence of the designated first day of the week on or after the first day of the year. The value is 1. This value will make sure that there will never be any odd weeks in the list of calculated weeks, but a week may span across the year boundary.
3. FirstFourDayWeek: Indicates that the first week of the year is the first week with four or more days before the designated first day of the week. The value is 2.

Disclaimer!: The following code is is in C# and not in VB.net. My proficiency in VB.net not good so I used the language I usually code in. I hope you'll be able to understand what I did. If you have any specific question about parts of the code you don't understand, I'll be happy to try to explain it to you. Not using VB.net has nothing to do with a like or dislike of that language.
C#
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace WeekCount
{
class Program
{
static void Main(string[] args)
{
DateTime start = new DateTime(2010, 1, 1);
DateTime end = new DateTime(2010, 12, 31);
DateTime current = start;
GregorianCalendar calendar = new GregorianCalendar(GregorianCalendarTypes.Localized);
CalendarWeekRule rule = CalendarWeekRule.FirstDay;
DayOfWeek firstDayOfWeek = DayOfWeek.Monday;
List<Week> weeks = Week.GetAllWeeks(
start,
end,
rule,
firstDayOfWeek,
GregorianCalendarTypes.Localized);

// This block will filter the weeks list so only weeks with a odd number of days
// will be output
List<Week> oddWeeks = weeks.FindAll(delegate(Week week)
{
return week.IsOdd;
});
int total = weeks.Count;
String verb1 = total > 1 ? "are a total of" : "is";
String plural1 = total > 1 ? "s" : "";
int odd = oddWeeks.Count;
String verb2   = odd > 1 || odd == 0 ? "are" : "is an";
String plural2 = odd > 1 || odd == 0 ? "s" : "";
String formatStatistics = "There {0} {1} week{2} in the list of which {3} {4} odd week{5}.\n";
Console.WriteLine(formatStatistics,
new Object[] {verb1, total, plural1, odd, verb2, plural2});
String formatLoop = "Start: {0:ddd MMM dd.} End: {1:ddd MMM dd.} Number: {2:00} Days: {3:00} Year: {4:####}";
foreach (Week week in oddWeeks)
{
Console.WriteLine(formatLoop, new Object[] {
week.Start,
week.End,
week.Number,
week.Days,
week.Year}
);
}
}
}
public class Week
{
public DateTime Start { get; set; }                                 // Start date of the week
public DateTime End { get; set; }                                   // End date of the week
public int Number { get; set; }                                     // Number of week
public int Days { get { return End.Subtract(Start).Days + 1; } }    // How many days does the week have
public bool IsOdd { get { return Days < 7; } }                   // A week is odd if it has less than 7 days
public int Year { get { return Start.Year; } }                      // Which year does this week belong to

public Week(DateTime start, DateTime end, int weekNumber)
{
this.Start  = new DateTime(start.Year, start.Month, start.Day);
this.End    = new DateTime(end.Year, end.Month, end.Day);
this.Number = weekNumber;
}

public bool Contains(DateTime date)
{
DateTime tmp = new DateTime(date.Year, date.Month, date.Day);
return Start <= tmp && tmp <= End;
}

public static List<Week> GetAllWeeks(
DateTime start,
DateTime end,
CalendarWeekRule rule,
DayOfWeek firstDayOfWeek,
GregorianCalendarTypes calendarType)
{
GregorianCalendar calendar = new GregorianCalendar(calendarType);
List<Week> result = new List<Week>();
DateTime currentDate = start;
// Find start date of current week of year
int currentWeekNumber = calendar.GetWeekOfYear(currentDate, rule, firstDayOfWeek);
do
{
} while (currentWeekNumber == calendar.GetWeekOfYear(currentDate, rule, firstDayOfWeek));
Week currentWeek = null;
DateTime startDateOfWeek = currentDate;
int nextWeekNumber = currentWeekNumber;
while (currentDate < end)
{
if (currentWeekNumber != calendar.GetWeekOfYear(currentDate, rule, firstDayOfWeek))
{
currentWeek = new Week(startDateOfWeek, currentDate.AddDays(-1), currentWeekNumber);
currentWeekNumber = calendar.GetWeekOfYear(currentDate, rule, firstDayOfWeek);
startDateOfWeek = currentDate;
}
}
// We need to add another week if none are in the result or if we have a startDateOfWeek
// different from our last result
if (result.Count == 0 || startDateOfWeek != result.Last().Start)
{
// Now lets seek to the end of the week
while (currentWeekNumber == calendar.GetWeekOfYear(currentDate, rule, firstDayOfWeek))
{
}
}
return result;
}
}
}```

v2
Espen Harlinn 25-Jun-11 13:18pm
Excellent reply, wish I could award at least 2x5, good work 5++
Manfred Rudolf Bihy 25-Jun-11 13:57pm
Thanks Espen!
thatraja 25-Jun-11 13:27pm
Wow, Complete answer for this question. 5!

Manfred Rudolf Bihy 25-Jun-11 13:57pm
Thanks Raja!
Vivek Krishnamurthy 25-Jun-11 13:35pm
My 5.. Very detailed... :)

## Solution 2

VB
```Dim dateFrom As New DateTime(2006, 7, 1)
Dim dateTo As New DateTime(2011, 12, 31)
' Difference in days, hours, and minutes.
'NumberOfWeeks(oldDate, newDate)
Dim Span As TimeSpan = dateTo.Subtract(dateFrom)
If Span.Days <= 7 Then
If dateFrom.DayOfWeek > dateTo.DayOfWeek Then
End If

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 =DayCount + 7
WeekCount = WeekCount + 1
End While

Try dis..........

gani7787 25-Jun-11 9:08am
What changes did you do. can you pls. mark it out...
gani7787 25-Jun-11 9:11am
I need to get all weeks list number for the years

pls. help
Manfred Rudolf Bihy 25-Jun-11 13:12pm
Please use Calendar.GetWeekOfYear to calculate the week number. The way you're doing it here is just plain wrong.
See my elaborated example for that method and the nescessary parameters to adapt it to your locale.