我需要按日期范围过滤记录,可以是自定义的或预定义的。为此,我实现了以下代码,但我感觉它仍然可以优化。我想听听意见和建议。
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(-1 * diff).Date;
}
public enum DateRange
{
Last24Hours = 1,
Today = 2,
Yesterday = 3,
LastWeek = 4,
ThisWeek = 5,
LastMonth = 6,
Custom = 0
}
public class DateRangeFilter
{
internal static readonly DateTime DefaultDateTime = DateTime.MinValue;
private DateRange? _period;
public DateRange? Period
{
get
{
if (_reportStartDate != DefaultDateTime && _reportEndDate != DefaultDateTime)
{
_period = DateRange.Custom;
}
else
{
_period = !_period.HasValue ? DateRange.ThisWeek : _period;
}
return _period;
}
set
{
_period = value;
}
}
private DateTime _reportStartDate;
public DateTime StartDate
{
get { return Period == DateRange.Custom ? GetFormatedDate(_reportStartDate) : GetDateRange(true); }
set { _reportStartDate = value; }
}
private DateTime _reportEndDate;
public DateTime EndDate
{
get { return Period == DateRange.Custom ? GetFormatedDate(_reportEndDate, false) : GetDateRange(false); }
set { _reportEndDate = value; }
}
private static DateTime GetFormatedDate(DateTime dt, bool isStartDate = true)
{
var h = (isStartDate) ? 0 : 23;
var m = (isStartDate) ? 0 : 59;
var s = (isStartDate) ? 0 : 59;
return new DateTime(dt.Year, dt.Month, dt.Day, h, m, s);
}
private DateTime GetDateRange(bool isStartDate)
{
var returnDate = DateTime.UtcNow;
var y = DateTime.UtcNow.AddDays(-1);
var yesterday = new DateTime(y.Year, y.Month, y.Day, 23, 59, 59);
var h = (isStartDate) ? 0 : 23;
var m = (isStartDate) ? 0 : 59;
var s = (isStartDate) ? 0 : 59;
switch (Period)
{
case DateRange.Today:
returnDate = (isStartDate) ? new DateTime(returnDate.Year, returnDate.Month, returnDate.Day, h, m, s) : returnDate;
break;
case DateRange.Yesterday:
returnDate = (isStartDate) ? new DateTime(y.Year, y.Month, y.Day, h, m, s) : yesterday;
break;
case DateRange.Last24Hours:
returnDate = (isStartDate) ? returnDate.AddHours(-24) : returnDate;
break;
case DateRange.LastWeek:
var first = returnDate.AddDays(-(int)returnDate.DayOfWeek - 6);
var last = first.AddDays(6);
first = new DateTime(first.Year, first.Month, first.Day, h, m, s);
returnDate = (isStartDate) ? first : new DateTime(last.Year, last.Month, last.Day, h, m, s);
break;
case DateRange.ThisWeek:
var startOfWeek = returnDate.StartOfWeek(DayOfWeek.Monday);
var lastDayOfWeek = startOfWeek.AddDays(6);
lastDayOfWeek = new DateTime(lastDayOfWeek.Year, lastDayOfWeek.Month, lastDayOfWeek.Day, h, m, s);
returnDate = (isStartDate) ? startOfWeek : lastDayOfWeek;
break;
case DateRange.LastMonth:
var month = new DateTime(returnDate.Year, returnDate.Month, 1, 0, 0, 0);
var lastOfMonth = month.AddDays(-1);
lastOfMonth = new DateTime(lastOfMonth.Year, lastOfMonth.Month, lastOfMonth.Day, h, m, s);
returnDate = (isStartDate) ? month.AddMonths(-1) : lastOfMonth;
break;
case DateRange.Last30Days:
returnDate = (isStartDate) ? yesterday.AddDays(-30).AddSeconds(1) : yesterday;
break;
case DateRange.Last90Days:
returnDate = (isStartDate) ? yesterday.AddDays(-90).AddSeconds(1) : yesterday;
break;
}
return returnDate;
}
}
使用:
public GetRecords([FromUri]DateRangeFilter filter) { ... }