468,507 Members | 1,546 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,507 developers. It's quick & easy.

Wrong number of weeks in years

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
Nov 16 '05 #1
3 5262
I got the same result on a Swedish box and I used the beta to compile it
with.
---
Microsoft (R) Visual C# .NET Compiler version 8.00.40607.42
for Microsoft (R) Windows (R) .NET Framework version 2.0.40607
Copyright (C) Microsoft Corporation 2001-2003. All rights reserved.
---

Regards
Tony Schon

Soren Jorgensen wrote:
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

Nov 16 '05 #2
Soren Jorgensen wrote:
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

Nov 16 '05 #3
Hans Kesting wrote:
Soren Jorgensen wrote:
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).


sorry, typo: of course that should have been "dec 31, 2003" that is in 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

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Stig Andersen | last post: by
7 posts views Thread by Shuffs | last post: by
6 posts views Thread by CryingFreeman | last post: by
11 posts views Thread by Neo Morpheous | last post: by
14 posts views Thread by Tommy Jakobsen | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.