Datetime增加Between方法
This commit is contained in:
parent
5b7a438725
commit
4566a84d96
|
@ -16,8 +16,8 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="date">日期</param>
|
/// <param name="date">日期</param>
|
||||||
/// <param name="option">查询选项</param>
|
/// <param name="option">查询选项</param>
|
||||||
/// <returns>符合条件日期</returns>
|
/// <returns>符合条件日期</returns>
|
||||||
public static DateTime GetDay(this DateTime date, DateOption option) {
|
public static DateTime GetDay(this DateTime date,DateOption option) {
|
||||||
date = option.RetentionTime ? date : new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
|
date = option.RetentionTime ? date : new DateTime(date.Year,date.Month,date.Day,0,0,0);
|
||||||
var drt = option.DateRangeType;
|
var drt = option.DateRangeType;
|
||||||
var dt = option.DateType;
|
var dt = option.DateType;
|
||||||
var firstDayOfWeek = option.DayOfWeek;
|
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.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.FirstDay ? date.AddDays(1 - date.Day) :
|
||||||
drt == DateRangeType.Monthly && dt == DateType.LastDay ? date.AddDays(1 - date.Day).AddMonths(1).AddDays(-1) :
|
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.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.LastDay ? new DateTime(date.Year,1,1).AddYears(1).AddDays(-1) :
|
||||||
|
|
||||||
throw new NotSupportedException("不支持这个日期查询");
|
throw new NotSupportedException("不支持这个日期查询");
|
||||||
}
|
}
|
||||||
|
@ -47,8 +47,8 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="date">本周的一天</param>
|
/// <param name="date">本周的一天</param>
|
||||||
/// <param name="firstDayOfWeek">默认第一天是星期几</param>
|
/// <param name="firstDayOfWeek">默认第一天是星期几</param>
|
||||||
/// <returns>本周第一天</returns>
|
/// <returns>本周第一天</returns>
|
||||||
public static DateTime GetFirstDayOfTheWeek(this DateTime date, DayOfWeek firstDayOfWeek = DayOfWeek.Monday) {
|
public static DateTime GetFirstDayOfTheWeek(this DateTime date,DayOfWeek firstDayOfWeek = DayOfWeek.Monday) {
|
||||||
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Weekly, DateType = DateType.FirstDay, DayOfWeek = firstDayOfWeek, });
|
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Weekly,DateType = DateType.FirstDay,DayOfWeek = firstDayOfWeek,});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -57,7 +57,7 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="date">日期</param>
|
/// <param name="date">日期</param>
|
||||||
/// <returns>日期</returns>
|
/// <returns>日期</returns>
|
||||||
public static DateTime GetFirstDayOfTheMonth(this DateTime date) {
|
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>
|
/// <summary>
|
||||||
|
@ -66,7 +66,7 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="date">日期</param>
|
/// <param name="date">日期</param>
|
||||||
/// <returns>日期</returns>
|
/// <returns>日期</returns>
|
||||||
public static DateTime GetFirstDayOfTheYear(this DateTime date) {
|
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>
|
/// <summary>
|
||||||
|
@ -75,8 +75,8 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="date">日期</param>
|
/// <param name="date">日期</param>
|
||||||
/// <param name="firstDayOfWeek">第一天是星期几</param>
|
/// <param name="firstDayOfWeek">第一天是星期几</param>
|
||||||
/// <returns>第几周</returns>
|
/// <returns>第几周</returns>
|
||||||
public static int GetWeekOfTheYear(this DateTime date, DayOfWeek firstDayOfWeek = DayOfWeek.Monday) {
|
public static int GetWeekOfTheYear(this DateTime date,DayOfWeek firstDayOfWeek = DayOfWeek.Monday) {
|
||||||
return date.GetDefaultCalendar().GetWeekOfYear(date, CalendarWeekRule.FirstDay, firstDayOfWeek);
|
return date.GetDefaultCalendar().GetWeekOfYear(date,CalendarWeekRule.FirstDay,firstDayOfWeek);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -137,12 +137,24 @@ namespace Falcon.SugarApi
|
||||||
public static string ToDateTime(this DateTime date) => date.ToString("yyyy-MM-dd HH:mm:ss");
|
public static string ToDateTime(this DateTime date) => date.ToString("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
#endregion
|
#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>
|
||||||
/// 日期操作选项
|
/// 日期操作选项
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DateOption : IOptions<DateOption>
|
public class DateOption:IOptions<DateOption>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 日期类型,默认第一天
|
/// 日期类型,默认第一天
|
||||||
|
|
Loading…
Reference in New Issue
Block a user