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.Globaliz ation.Gregorian Calendar cal =
new System.Globaliz ation.Gregorian Calendar();
DateTime d;
int week;
for (int year=2000;year < 2005; year++)
{
d = new DateTime(year, 12, 31, cal);
week = cal.GetWeekOfYe ar(d,
System.Globaliz ation.CalendarW eekRule.FirstFo urDayWeek,
System.DayOfWee k.Monday);
Console.WriteLi ne ("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.GetWeekOfYe ar(d,
ci.DateTimeForm at.CalendarWeek Rule,
ci.DateTimeForm at.FirstDayOfWe ek);
Console.WriteLi ne ("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.Globaliz ation.Gregorian Calendar cal =
new System.Globaliz ation.Gregorian Calendar();
DateTime d;
int week;
for (int year=2000;year < 2005; year++)
{
d = new DateTime(year, 12, 31, cal);
week = cal.GetWeekOfYe ar(d,
System.Globaliz ation.CalendarW eekRule.FirstFo urDayWeek,
System.DayOfWee k.Monday);
Console.WriteLi ne ("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.GetWeekOfYe ar(d,
ci.DateTimeForm at.CalendarWeek Rule,
ci.DateTimeForm at.FirstDayOfWe ek);
Console.WriteLi ne ("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
Comment