2

I have a method that checks how many bookings I have in the current month:

if (item.DateTimeScheduled.Month == DateTime.Now.Month)

Is it possible to do something similar for the current week? How can I check the number of bookings done in the current week?

tereško
  • 58,060
  • 25
  • 98
  • 150
user2915962
  • 2,691
  • 8
  • 33
  • 60

1 Answers1

1

If you have a collection of "Items" with a DateTime property and you want to count all of this week, this should work:

var calendar = DateTimeFormatInfo.CurrentInfo.Calendar;
var weekRule = DateTimeFormatInfo.CurrentInfo.CalendarWeekRule;
var firstDay = DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek;
int week = calendar.GetWeekOfYear(DateTime.Today, weekRule, firstDay);
int thisWeekBookings = items
    .Count(i => i.DateTimeScheduled.Year == DateTime.Today.Year &&
        week == calendar.GetWeekOfYear(i.DateTimeScheduled, weekRule, firstDay));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939