GetWeekOfYear problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pål Andreassen

    GetWeekOfYear problems

    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
  • Magnus Krisell

    #2
    Re: GetWeekOfYear problems

    Pål,

    It seems you are assuming that 31 December always belongs to
    either week 52 or week 53, but that is not true.
    For example, 31 December 2001 was in week 1 of 2002.
    GetWeekOfYear() returns 53 in such cases, which can be confusing.
    To find out if the week should indeed be 53 or if it should be week 1
    of the next year, check the weekday of 31 December. If it's a Monday,
    Tuseday or Wednesday, the year has only 52 weeks. If it's a
    Thursday or Friday (it will never be a Saturday or Sunday), then
    the year has 53 weeks.

    Using your first example, change the code to:

    for (int year = 1980; year <= 2010; year++)
    {
    d = new DateTime(year, 12, 31, cal);

    week = cal.GetWeekOfYe ar(d,
    System.Globaliz ation.CalendarW eekRule.FirstFo urDayWeek,
    System.DayOfWee k.Monday);

    if (week == 53 && (d.DayOfWeek == DayOfWeek.Monda y ||
    d.DayOfWeek == DayOfWeek.Tuesd ay ||
    d.DayOfWeek == DayOfWeek.Wedne sday))
    {
    week = 52;
    }

    Console.WriteLi ne ("Year: " + year.ToString() + ", weeks: " +
    week);
    }

    There might be a more elegant way to solve this problem, but this at least
    produces the correct results.

    - Magnus

    "Pål Andreassen" <pananza@hotmai l.com> wrote in message
    news:ba312888.0 309100226.7cf1c 932@posting.goo gle.com...[color=blue]
    > 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[/color]


    Comment

    • Morten Wennevik

      #3
      Re: GetWeekOfYear problems

      Another method is to check if January 1st is on week 1, AND if the next
      years january is NOT on week 1, then you have 53 weeks

      foreach(int year = 2000; year < 2005; year++)
      {
      DateTime d1 = new DateTime(year, 1, 1, cal);
      DateTime d2 = new DateTime(year+1 , 1, 1, cal);
      if(cal.GetDayOf Week(d1) <= System.DayOfWee k.Thursday &
      cal.GetDayOfWee k(d2) > System.DayOfWee k.Thursday)
      // 53 weeks
      else
      // 52 weeks
      }

      However, I'm not 100% sure this works for all cases, since despite having
      localized calendar formats (I also use the Norwegian settings) .Net
      Framework will think DayOfWeek.Sunda y comes before DayOfWeek.Monda y. Odd
      since ISO8601 clearly states day 1 is Monday.

      I encountered this problem when doing a listing of all cultures, and no
      matter what culture, CultureInfo.Dat eTimeFormat.Day Names listed Sunday as
      the first day.

      --
      Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

      Comment

      • Pål Andreassen

        #4
        Re: GetWeekOfYear problems

        "Magnus Krisell" <magkr747@NOSPA Mstudent.liu.se > wrote in
        news:uJ6Kdz7dDH A.3228@tk2msftn gp13.phx.gbl:[color=blue]
        >It seems you are assuming that 31 December always belongs to
        >either week 52 or week 53, but that is not true.
        >For example, 31 December 2001 was in week 1 of 2002.
        >GetWeekOfYear( ) returns 53 in such cases, which can be confusing.
        >To find out if the week should indeed be 53 or if it should be week 1
        >of the next year, check the weekday of 31 December. If it's a Monday,
        >Tuseday or Wednesday, the year has only 52 weeks. If it's a
        >Thursday or Friday (it will never be a Saturday or Sunday), then
        >the year has 53 weeks.[/color]

        Thank you very much. You saved me from ripping out the rest of my hair.

        --
        Pål Andreassen
        cnny.naqernffra @gevznarg.ab
        (ROT13 to reply)

        Comment

        • Petteri

          #5
          Re: GetWeekOfYear problems

          Magnus,

          I think that there is a bug in the GetWeekOfYear function.
          What is the point of CalendarWeekRul e and DayOfWeek
          parameters if I have to find out the correct week number
          myself?

          Best Regards
          Petteri

          Comment

          Working...