Wrong number of weeks in years

  • Thread starter Thread starter Soren Jorgensen
  • Start date Start date
S

Soren Jorgensen

Hi,

Following code should give the number of weeks in years 1998-2010 for a
Danish calendar (on a Danish box)

GregorianCalendar cal = new GregorianCalendar();
for(int i = 1998; i < 2010; i++)
{
DateTime date = new DateTime(i, 12, 31);
int week= cal.GetWeekOfYear(date,
CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
Debug.WriteLine( String.Format("Last week in year {0:0} = {1:0}", date.Year,
week));
}

Last week in year 1998 = 53 - OK
Last week in year 1999 = 52 - OK
Last week in year 2000 = 52 - OK
Last week in year 2001 = 53 - Wrong, only 52
Last week in year 2002 = 53 - Wrong, only 52
Last week in year 2003 = 53 - Wrong, only 52
Last week in year 2004 = 53 - OK
Last week in year 2005 = 52 - OK
Last week in year 2006 = 52 - OK
Last week in year 2007 = 53 - Wrong, only 52
Last week in year 2008 = 53 - Wrong, only 52
Last week in year 2009 = 53 - Wrong, only 52

Does anyone see the same pattern as I, and do anyone know why this happens
??

Kind regards, and thanx in advance...

Soren
 
I got the same result on a Swedish box and I used the beta to compile it
with.
 
Soren said:
Hi,

Following code should give the number of weeks in years 1998-2010 for
a Danish calendar (on a Danish box)

GregorianCalendar cal = new GregorianCalendar();
for(int i = 1998; i < 2010; i++)
{
DateTime date = new DateTime(i, 12, 31);
int week= cal.GetWeekOfYear(date,
CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
Debug.WriteLine( String.Format("Last week in year {0:0} = {1:0}",
date.Year, week));
}

Last week in year 1998 = 53 - OK
Last week in year 1999 = 52 - OK
Last week in year 2000 = 52 - OK
Last week in year 2001 = 53 - Wrong, only 52
Last week in year 2002 = 53 - Wrong, only 52
Last week in year 2003 = 53 - Wrong, only 52
Last week in year 2004 = 53 - OK
Last week in year 2005 = 52 - OK
Last week in year 2006 = 52 - OK
Last week in year 2007 = 53 - Wrong, only 52
Last week in year 2008 = 53 - Wrong, only 52
Last week in year 2009 = 53 - Wrong, only 52

Does anyone see the same pattern as I, and do anyone know why this
happens ??

Kind regards, and thanx in advance...

Soren

When I look up the weeknumbers in my calendar (dutch, same rules as you used),
dec 31, 2004 is reported as week 1 (of 2004).
Week 52 of 2003 ends on sunday dec 28, so a result of 53 for dec 31 can be
explained (but is still wrong).

Maybe you need to change your algorithm: is dec 31 a monday through wednesday,
then look a week earier for the last "legal" weeknumber.

Hans Kesting
 
Hans said:
When I look up the weeknumbers in my calendar (dutch, same rules as
you used), dec 31, 2004 is reported as week 1 (of 2004).

sorry, typo: of course that should have been "dec 31, 2003" that is in week 1 of 2004
 
Back
Top