GetWeekOfYear problems

  • Thread starter Thread starter =?ISO-8859-1?Q?P=E5l_Andreassen?=
  • Start date Start date
?

=?ISO-8859-1?Q?P=E5l_Andreassen?=

I'm trying to discover the number of weeks in a given year. As I live
in Norway we are using the gregorian calendar. First week of a year is
the first week with four days. First day of the week is monday.

I've tried two different methods, both resulting in the same wrong
answer. Both methods tries to find the week number of the date 31.
Descember.

Try 1: Using the calendar type and rules manually

System.Globalization.GregorianCalendar cal =
new System.Globalization.GregorianCalendar();

DateTime d;
int week;

for (int year=2000;year < 2005; year++)
{
d = new DateTime(year, 12, 31, cal);

week = cal.GetWeekOfYear(d,

System.Globalization.CalendarWeekRule.FirstFourDayWeek,
System.DayOfWeek.Monday);

Console.WriteLine ("Year: " + year.ToString() + ", weeks: " +
week);
}

Try 2: Using CultureInfo

CultureInfo ci = new CultureInfo("nb-NO");
Calendar cal = ci.Calendar;
DateTime d;
int week;

for (int year=2000;year < 2005; year++)
{
d = new DateTime(year, 12, 31, cal);

week = cal.GetWeekOfYear(d,
ci.DateTimeFormat.CalendarWeekRule,
ci.DateTimeFormat.FirstDayOfWeek);

Console.WriteLine ("Year: " + year.ToString() + ", weeks: " +
week);
}

Both outputs:

Year: 2000, weeks: 52 (correct)
Year: 2001, weeks: 53 (wrong, should be 52)
Year: 2002, weeks: 53 (wrong, should be 52)
Year: 2003, weeks: 53 (wrong, should be 52)
Year: 2004, weeks: 53 (correct)

Any ideas? This is really driving me mad.

/Pål
 
Pål,

It seems you are assuming that 31 December always belongs to
either week 52 or week 53, but that is not true.
For example, 31 December 2001 was in week 1 of 2002.
GetWeekOfYear() returns 53 in such cases, which can be confusing.
To find out if the week should indeed be 53 or if it should be week 1
of the next year, check the weekday of 31 December. If it's a Monday,
Tuseday or Wednesday, the year has only 52 weeks. If it's a
Thursday or Friday (it will never be a Saturday or Sunday), then
the year has 53 weeks.

Using your first example, change the code to:

for (int year = 1980; year <= 2010; year++)
{
d = new DateTime(year, 12, 31, cal);

week = cal.GetWeekOfYear(d,
System.Globalization.CalendarWeekRule.FirstFourDayWeek,
System.DayOfWeek.Monday);

if (week == 53 && (d.DayOfWeek == DayOfWeek.Monday ||
d.DayOfWeek == DayOfWeek.Tuesday ||
d.DayOfWeek == DayOfWeek.Wednesday))
{
week = 52;
}

Console.WriteLine ("Year: " + year.ToString() + ", weeks: " +
week);
}

There might be a more elegant way to solve this problem, but this at least
produces the correct results.

- Magnus
 
Another method is to check if January 1st is on week 1, AND if the next
years january is NOT on week 1, then you have 53 weeks

foreach(int year = 2000; year < 2005; year++)
{
DateTime d1 = new DateTime(year, 1, 1, cal);
DateTime d2 = new DateTime(year+1, 1, 1, cal);
if(cal.GetDayOfWeek(d1) <= System.DayOfWeek.Thursday &
cal.GetDayOfWeek(d2) > System.DayOfWeek.Thursday)
// 53 weeks
else
// 52 weeks
}

However, I'm not 100% sure this works for all cases, since despite having
localized calendar formats (I also use the Norwegian settings) .Net
Framework will think DayOfWeek.Sunday comes before DayOfWeek.Monday. Odd
since ISO8601 clearly states day 1 is Monday.

I encountered this problem when doing a listing of all cultures, and no
matter what culture, CultureInfo.DateTimeFormat.DayNames listed Sunday as
the first day.
 
Magnus Krisell said:
It seems you are assuming that 31 December always belongs to
either week 52 or week 53, but that is not true.
For example, 31 December 2001 was in week 1 of 2002.
GetWeekOfYear() returns 53 in such cases, which can be confusing.
To find out if the week should indeed be 53 or if it should be week 1
of the next year, check the weekday of 31 December. If it's a Monday,
Tuseday or Wednesday, the year has only 52 weeks. If it's a
Thursday or Friday (it will never be a Saturday or Sunday), then
the year has 53 weeks.

Thank you very much. You saved me from ripping out the rest of my hair.
 
Magnus,

I think that there is a bug in the GetWeekOfYear function.
What is the point of CalendarWeekRule and DayOfWeek
parameters if I have to find out the correct week number
myself?

Best Regards
Petteri
 
Back
Top