?
=?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
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