Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use following two methods to calculate the week-number and the start-date of a given weeknumber according to a given year:</p> <pre><code>// this method is borrowed from http://stackoverflow.com/a/11155102/284240 public static int GetIso8601WeekOfYear(DateTime time) { DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time); if (day &gt;= DayOfWeek.Monday &amp;&amp; day &lt;= DayOfWeek.Wednesday) { time = time.AddDays(3); } return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); } public static DateTime FirstDateOfWeek(int year, int weekOfYear, System.Globalization.CultureInfo ci) { DateTime jan1 = new DateTime(year, 1, 1); int daysOffset = (int)ci.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek; DateTime firstWeekDay = jan1.AddDays(daysOffset); int firstWeek = ci.Calendar.GetWeekOfYear(jan1, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek); if ((firstWeek &lt;= 1 || firstWeek &gt;= 52) &amp;&amp; daysOffset &gt;= -3) { weekOfYear -= 1; } return firstWeekDay.AddDays(weekOfYear * 7); } </code></pre> <p>Then you can get both dates in the following way:</p> <pre><code>// 46 int thisWeekNumber = GetIso8601WeekOfYear(DateTime.Today); // 11/11/2013 DateTime firstDayOfWeek= FirstDateOfWeek(2013, thisWeekNumber, CultureInfo.CurrentCulture); // 11/12/2012 DateTime firstDayOfLastYearWeek = FirstDateOfWeek(2012, thisWeekNumber, CultureInfo.CurrentCulture); </code></pre> <p>Add 6 days to get the end of the week.</p>
 

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