You are here

Get the first day of a given week (ISO 8601)

Most programming languages provide a way to get the week number of a given date. For an SQL example, check out my previous article: SQL: year of an ISO week.

In C# you can use the following:

int week = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

But there's usually no function provided to get the first day of a given week and year. The shortest (but still readable) way that I can get up with in C# is the following:

DateTime getMonday(int year, int week)
// 4 January is always in week 1 (see
DateTime jan4 = new DateTime(year, 1, 4);

// get a day in the requested week
DateTime day = jan4.AddDays((week - 1) * 7);

// get day of week, with [mon = 0 ... sun = 6] instead of [sun = 0 ... sat = 6]
int dayOfWeek = ((int)day.DayOfWeek + 6) % 7;

return day.AddDays(-dayOfWeek);


Great code!