Guffa | Foto | Programmering

GetWeekOfYear

// Bugfix replacement for System.Globalization.Calendar.GetWeekOfYear(date, FirstFourDayWeek, Monday)
// Copyright 2005 Göran Andersson
public static int GetWeekOfYear(DateTime date) {
   DateTime first, start;
   int year;

   year = date.Year + 1;
   do {
      first = new DateTime(year--, 1, 1);
      start = first.AddDays(3 - (((int)first.DayOfWeek + 2) % 7));
   } while (date < start);
   return ((int)date.Subtract(start).TotalDays) / 7 + 1;
}