項目需求:
某學校訂單截止操作時間的上一個月最后一天晚上23:59:59 為止所有支付的訂單統計;
代碼:
/// <summary>
/// 通過學校和截止時間得到訂單
/// </summary>
/// <param name="pageIndex">頁個數</param>
/// <param name="pageSize">頁面積</param>
/// <param name="schoolId">學校ID</param>
/// <param name="orderSettlementEndTime">截止時間</param>
/// <returns></returns>
public PagedList<Order> GetOrderBySchoolAndEndTime(int pageIndex,int pageSize,Guid? schoolId, DateTime? orderSettlementEndTime)
{
var query = this.Table;
if (schoolId.HasValue)
{
query = query.Where(x => x.SchoolId == schoolId);
}
if (orderSettlementEndTime.HasValue)
{
//本月的最后一天
var endMonth = DateTime.Parse(
orderSettlementEndTime.Value.AddDays(1 - orderSettlementEndTime.Value.Day)
.AddMonths(1)
.AddDays(-1)
.ToString());
//上月的最后一天
var startMonth =
DateTime.Parse(
orderSettlementEndTime.Value.AddDays(1 - orderSettlementEndTime.Value.Day)
.AddSeconds(-1).ToString());
query = query.Where(x => x.CreateTime >= startMonth&&x.CreateTime<endMonth);
}
return query.Where(x => x.PaymentState == PaymentState.支付完成)
.OrderBy(x=>x.Id)
.ToPagedList(pageIndex,pageSize);
}
圖片:
利用DateTime.Parse();將string類型的時間轉換為datetime類型,我們看一下后面的代碼,是將時間手動的設置為我們需要的時間。
DateTime today12hours = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 12:00:00"));
這樣我們設置了值。
封裝取時間的方法
/// <summary>
/// 某月的最后一天
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public DateTime FirstDayOfMonth(DateTime datetime)
{
return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
}
/// <summary>
/// 前一月的最后一天
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public DateTime LastDayOfMonth(DateTime datetime)
{
return datetime.AddDays(1 - datetime.Day).AddSeconds(-1);
}
/// <summary>
/// 某月的第一天
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public DateTime FirstDayOfMonthMethod(DateTime datetime)
{
return datetime.AddDays(1 - datetime.Day);
}
/// <summary>
/// 上一月的第一天
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public DateTime FirstDayOfUpMouth(DateTime datetime)
{
return datetime.AddDays(1 - datetime.Day).AddMonths(-1);
}
/// <summary>
/// 取得上個月的最后一天
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public DateTime LastDayOfOnMouth(DateTime datetime)
{
return datetime.AddDays(1 - datetime.Day).AddDays(-1);
}


