Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>var currentCulture = CultureInfo.CurrentCulture; var weekNo = currentCulture.Calendar.GetWeekOfYear( new DateTime(2013, 12, 31), currentCulture.DateTimeFormat.CalendarWeekRule, currentCulture.DateTimeFormat.FirstDayOfWeek); </code></pre> <p>Be aware that this is <strong>not</strong> <a href="http://en.wikipedia.org/wiki/ISO_8601#Week_dates" rel="noreferrer">ISO 8601</a> compatible. In Sweden we use ISO 8601 <a href="http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx" rel="noreferrer">week</a> numbers but even though the culture is set to "sv-SE", <code>CalendarWeekRule</code> is <code>FirstFourDayWeek</code>, and <code>FirstDayOfWeek</code> is Monday the <em>weekNo</em> variable will be set to <strong>53</strong> instead of the correct <strong>1</strong> in the above code.</p> <p>I have only tried this with Swedish settings but I'm pretty sure that all countries (Austria, Germany, Switzerland and more) using ISO 8601 week numbers will be affected by this problem.</p> <p><a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2005/09/26/132466.aspx" rel="noreferrer">Peter van Ooijen</a> and <a href="http://blogs.msdn.com/shawnste/archive/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net.aspx" rel="noreferrer">Shawn Steele</a> has different solutions to this problem.</p> <p>Here's a compact solution</p> <pre><code>private static int WeekOfYearISO8601(DateTime date) { var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date); return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); } </code></pre> <p>It's been tested for the following dates</p> <pre><code>var datesAndISO8601Weeks = new Dictionary&lt;DateTime, int&gt; { {new DateTime(2000, 12, 31), 52}, {new DateTime(2001, 1, 1), 1}, {new DateTime(2005, 1, 1), 53}, {new DateTime(2007, 12, 31), 1}, {new DateTime(2008, 12, 29), 1}, {new DateTime(2010, 1, 3), 53}, {new DateTime(2011, 12, 31), 52}, {new DateTime(2012, 1, 1), 52}, {new DateTime(2013, 1, 2), 1}, {new DateTime(2013, 12, 31), 1}, }; foreach (var dateWeek in datesAndISO8601Weeks) { Debug.Assert(WeekOfYearISO8601(dateWeek.Key) == dateWeek.Value, dateWeek.Key.ToShortDateString() + " should be week number " + dateWeek.Value + " but was " + WeekOfYearISO8601(dateWeek.Key)); } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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