Note that there are some explanatory texts on larger screens.

plurals
  1. POIs .NET giving me the wrong week number for Dec. 29th 2008?
    primarykey
    data
    text
    <p>According to the <a href="http://www.timeanddate.com/calendar/custom.html?year=2008&amp;country=22&amp;month=12&amp;typ=2&amp;months=2&amp;display=0&amp;space=0&amp;fdow=1&amp;wno=1&amp;hol=" rel="noreferrer">official (gregorian) calendar</a>, the week number for 29/12/2008 is 1, because after the last day of week 52 (i.e. 28/12) there are three or less days left in the year. Kinda weird, but OK, rules are rules.</p> <p>So according to this calendar, we have these boundary values for 2008/2009</p> <ul> <li>28/12 is week 52</li> <li>29/12 is week 1</li> <li>1/1 is week 1</li> <li>8/1 is week 2</li> </ul> <p>C# offers a GregorianCalendar class, that has a function <code>GetWeekOfYear(date, rule, firstDayOfWeek)</code>. </p> <p>The parameter <code>rule</code> is an enumeration with 3 possible values: <code>FirstDay, FirstFourWeekDay, FirstFullWeek</code>. From what I've understood I should go for the <code>FirstFourWeekDay</code> rule, but I tried all of them just in case.</p> <p>The last parameter informs which week day should be considered the first day of the week, according to that calendar it's Monday so Monday it is.</p> <p>So I fired up a quick and dirty console app to test this:</p> <pre><code>using System; using System.Globalization; namespace CalendarTest { class Program { static void Main(string[] args) { var cal = new GregorianCalendar(); var firstWeekDay = DayOfWeek.Monday; var twentyEighth = new DateTime(2008, 12, 28); var twentyNinth = new DateTime(2008, 12, 29); var firstJan = new DateTime(2009, 1, 1); var eightJan = new DateTime(2009, 1, 8); PrintWeekDays(cal, twentyEighth, firstWeekDay); PrintWeekDays(cal, twentyNinth, firstWeekDay); PrintWeekDays(cal, firstJan, firstWeekDay); PrintWeekDays(cal, eightJan, firstWeekDay); Console.ReadKey(); } private static void PrintWeekDays(Calendar cal, DateTime dt, DayOfWeek firstWeekDay) { Console.WriteLine("Testing for " + dt.ToShortDateString()); Console.WriteLine("--------------------------------------------"); Console.Write(CalendarWeekRule.FirstDay.ToString() + "\t\t"); Console.WriteLine(cal.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, firstWeekDay)); Console.Write(CalendarWeekRule.FirstFourDayWeek.ToString() + "\t"); Console.WriteLine(cal.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, firstWeekDay)); Console.Write(CalendarWeekRule.FirstFullWeek.ToString() + "\t\t"); Console.WriteLine(cal.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, firstWeekDay)); Console.WriteLine("--------------------------------------------"); } } } </code></pre> <p>... and this what I get</p> <pre><code>Testing for 28.12.2008 -------------------------------------------- FirstDay 52 FirstFourDayWeek 52 FirstFullWeek 51 -------------------------------------------- Testing for 29.12.2008 -------------------------------------------- FirstDay 53 FirstFourDayWeek 53 FirstFullWeek 52 -------------------------------------------- Testing for 01.01.2009 -------------------------------------------- FirstDay 1 FirstFourDayWeek 1 FirstFullWeek 52 -------------------------------------------- Testing for 08.01.2009 -------------------------------------------- FirstDay 2 FirstFourDayWeek 2 FirstFullWeek 1 -------------------------------------------- </code></pre> <p>So as we see, none of the combinations above matches the official calendar (if you are in a hurry, just see that 29/12 never gets week #1).</p> <p>What am I getting wrong here? Maybe there's something glaring that I am missing? (it's Friday and late work hours here in Belgium, bear with me ;))</p> <p>Edit: Maybe I should explain: what I need is a function that works for any year, returning the same results as the gregorian calendar I linked. So no special workarounds for 2008. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload