使用DateTime检查当前周的预订

时间:2014-03-28 21:18:44

标签: c# datetime

我有一种方法可以检查当月有多少预订:

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

本周有可能做类似的事吗?如何查看当周完成的预订次数?

1 个答案:

答案 0 :(得分:1)

如果你有一个带有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));