Yılın kaçıncı haftasındayız?
Belki birgün lazım olur :)
Sizinle yılın kaçıncı haftasında olduğumuzu hesaplayan bir extension metod göstermek istiyorum:
public static class ExtensionMethods { ////// Offsets to move the day of the year on a week, allowing /// for the current year Jan 1st day of week, and the Sun/Mon /// week start difference between ISO 8601 and Microsoft /// private static int[] moveByDays = { 6, 7, 8, 9, 10, 4, 5 }; /// /// Get the Week number of the year /// (In the range 1..53) /// This conforms to ISO 8601 specification for week number. /// /// /// Week of year public static int WeekOfYear(this DateTime date) { DateTime startOfYear = new DateTime(date.Year, 1, 1); DateTime endOfYear = new DateTime(date.Year, 12, 31); // ISO 8601 weeks start with Monday // The first week of a year includes the first Thursday // This means that Jan 1st could be in week 51, 52, or 53 of the previous year... int numberDays = date.Subtract(startOfYear).Days + moveByDays[(int) startOfYear.DayOfWeek]; int weekNumber = numberDays / 7; switch (weekNumber) { case 0: // Before start of first week of this year - in last week of previous year weekNumber = WeekOfYear(startOfYear.AddDays(-1)); break; case 53: // In first week of next year. if (endOfYear.DayOfWeek < DayOfWeek.Thursday) { weekNumber = 1; } break; } return weekNumber; } }
Ne işime yarar demeyin, belki bir gün lazım olur:)
Kullanım şekli:
int hafta = DateTime.Today.WeekOfYear();
Başka bir alternatif yol:
CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(tarih, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
Herkese kolay gelsin..