473,378 Members | 1,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 5458
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stig Andersen | last post by:
Hi does anyone know how to make MS SQL server 2000 calculate the right week number around new year ? According to the help file, MSSQL should follow ISO8601, which ensures that i.e. 2003 has...
7
by: Shuffs | last post by:
Could someone, anyone please tell me what I need to amend, to get this function to take Sunday as the first day of the week? I amended the Weekday parts to vbSunday (in my code, not the code...
6
by: CryingFreeman | last post by:
Hi everyone, I started in access just a copple of weeks ago, to automise my ecell DVD database.So I could make forms, ans reports.. Thought it would be easy, but I'm trying nog for weeks and I...
9
by: Alan Silver | last post by:
hello, I am writing my first real ASP.NET application, but am getting a bit confused with ADO.NEt and the best way to use it. My old method (with Classic ASP) was to have a function that you...
11
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation a table with columns and rows
2
by: Bull | last post by:
Hi, Is there a code that converts a whole number to Years and Weeks only? Thanks, Bull
11
by: Neo Morpheous | last post by:
Ok, first lets start with some definitions: By "Expert", I mean someone who : 1). Is familiar with and understands the *MAJOR* concepts/philosopies underlying C# (and possible .Net as a whole...
14
by: Tommy Jakobsen | last post by:
Hi. Is there a method in .NET that takes "year" as an argument and returns the total number of weeks in that year? For culture da-DK (Danish). Thanks in advance. Tommy.
4
by: Vince | last post by:
Given a week Number, how do I calculate the date that for the Monday of that week?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.