项目需求:
某学校订单截止操作时间的上一个月最后一天晚上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); }
时间: 2024-11-06 09:37:15