我有一个检查当月有多少预订的方法:
if (item.DateTimeScheduled.Month == DateTime.Now.Month)
本周是否有可能做类似的事情?如何查看本周完成的预订数量?
如果你有一个带有DateTime
属性的"Items"集合,并且你想计算本周的所有时间,这应该可以工作:
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));