Datetime增加Between方法

This commit is contained in:
FalconFly 2024-03-22 16:20:56 +08:00
parent 5b7a438725
commit 4566a84d96

View File

@ -16,8 +16,8 @@ namespace Falcon.SugarApi
/// <param name="date">日期</param>
/// <param name="option">查询选项</param>
/// <returns>符合条件日期</returns>
public static DateTime GetDay(this DateTime date, DateOption option) {
date = option.RetentionTime ? date : new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
public static DateTime GetDay(this DateTime date,DateOption option) {
date = option.RetentionTime ? date : new DateTime(date.Year,date.Month,date.Day,0,0,0);
var drt = option.DateRangeType;
var dt = option.DateType;
var firstDayOfWeek = option.DayOfWeek;
@ -26,8 +26,8 @@ namespace Falcon.SugarApi
drt == DateRangeType.Weekly && dt == DateType.LastDay ? date.AddDays(firstDayOfWeek - date.DayOfWeek + 7) :
drt == DateRangeType.Monthly && dt == DateType.FirstDay ? date.AddDays(1 - date.Day) :
drt == DateRangeType.Monthly && dt == DateType.LastDay ? date.AddDays(1 - date.Day).AddMonths(1).AddDays(-1) :
drt == DateRangeType.Yearly && dt == DateType.FirstDay ? new DateTime(date.Year, 1, 1) :
drt == DateRangeType.Yearly && dt == DateType.LastDay ? new DateTime(date.Year, 1, 1).AddYears(1).AddDays(-1) :
drt == DateRangeType.Yearly && dt == DateType.FirstDay ? new DateTime(date.Year,1,1) :
drt == DateRangeType.Yearly && dt == DateType.LastDay ? new DateTime(date.Year,1,1).AddYears(1).AddDays(-1) :
throw new NotSupportedException("不支持这个日期查询");
}
@ -47,8 +47,8 @@ namespace Falcon.SugarApi
/// <param name="date">本周的一天</param>
/// <param name="firstDayOfWeek">默认第一天是星期几</param>
/// <returns>本周第一天</returns>
public static DateTime GetFirstDayOfTheWeek(this DateTime date, DayOfWeek firstDayOfWeek = DayOfWeek.Monday) {
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Weekly, DateType = DateType.FirstDay, DayOfWeek = firstDayOfWeek, });
public static DateTime GetFirstDayOfTheWeek(this DateTime date,DayOfWeek firstDayOfWeek = DayOfWeek.Monday) {
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Weekly,DateType = DateType.FirstDay,DayOfWeek = firstDayOfWeek,});
}
/// <summary>
@ -57,7 +57,7 @@ namespace Falcon.SugarApi
/// <param name="date">日期</param>
/// <returns>日期</returns>
public static DateTime GetFirstDayOfTheMonth(this DateTime date) {
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Monthly, DateType = DateType.FirstDay });
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Monthly,DateType = DateType.FirstDay });
}
/// <summary>
@ -66,7 +66,7 @@ namespace Falcon.SugarApi
/// <param name="date">日期</param>
/// <returns>日期</returns>
public static DateTime GetFirstDayOfTheYear(this DateTime date) {
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Yearly, DateType = DateType.FirstDay });
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Yearly,DateType = DateType.FirstDay });
}
/// <summary>
@ -75,8 +75,8 @@ namespace Falcon.SugarApi
/// <param name="date">日期</param>
/// <param name="firstDayOfWeek">第一天是星期几</param>
/// <returns>第几周</returns>
public static int GetWeekOfTheYear(this DateTime date, DayOfWeek firstDayOfWeek = DayOfWeek.Monday) {
return date.GetDefaultCalendar().GetWeekOfYear(date, CalendarWeekRule.FirstDay, firstDayOfWeek);
public static int GetWeekOfTheYear(this DateTime date,DayOfWeek firstDayOfWeek = DayOfWeek.Monday) {
return date.GetDefaultCalendar().GetWeekOfYear(date,CalendarWeekRule.FirstDay,firstDayOfWeek);
}
/// <summary>
@ -137,12 +137,24 @@ namespace Falcon.SugarApi
public static string ToDateTime(this DateTime date) => date.ToString("yyyy-MM-dd HH:mm:ss");
#endregion
/// <summary>
/// 时间是否处于某个时间段内
/// </summary>
/// <param name="dt">要判断的时间</param>
/// <param name="start">开始时间</param>
/// <param name="end">结束时间</param>
/// <returns>True是False否</returns>
public static bool Between(this DateTime? dt,DateTime start,DateTime? end = null) {
end ??= DateTime.Now;
return dt > start && dt <= end;
}
}
/// <summary>
/// 日期操作选项
/// </summary>
public class DateOption : IOptions<DateOption>
public class DateOption:IOptions<DateOption>
{
/// <summary>
/// 日期类型,默认第一天