自然时间帮助类
/// <summary> /// 自然时间帮助类 /// </summary> public static class NaturalTimeTool { #region 获取自然周起止时间 /// <summary> /// 获取自然周起止时间 /// </summary> /// <param name="date"></param> /// <returns></returns> public static (DateTime startTime, DateTime endTime) GetWeekDate(DateTime date) { var startTime = date.AddDays(-(int.Parse(date.GetWeekNumberOfDay()) - 1)); var endTime = date.AddDays(7 - int.Parse(date.GetWeekNumberOfDay())); return (startTime, endTime); } #endregion #region 获取自然月起止时间 /// <summary> /// 获取自然月起止时间 /// </summary> /// <param name="date"></param> /// <returns></returns> public static (DateTime startTime, DateTime endTime) GetMonthDate(DateTime date) { var startTime = date.GetFirstDayOfMonth(); var endTime = date.GetLastDayOfMonth(); return (startTime, endTime); } #endregion #region 获取自然季度起止时间 /// <summary> /// 获取自然季度起止时间 /// </summary> /// <param name="date"></param> /// <returns></returns> public static (DateTime startTime, DateTime endTime) GetQuarterDate(DateTime date) { var startTime = new DateTime(); var endTime = new DateTime(); //第一季度 if (date.Month <= 3) { startTime = new DateTime(date.Year, 1, 1); endTime = new DateTime(date.Year, 3, 31); } //第二季度 else if (date.Month <= 6) { startTime = new DateTime(date.Year, 4, 1); endTime = new DateTime(date.Year, 6, 30); } //第三季度 else if (date.Month <= 9) { startTime = new DateTime(date.Year, 7, 1); endTime = new DateTime(date.Year, 9, 30); } //第四季度 else if (date.Month <= 12) { startTime = new DateTime(date.Year, 10, 1); endTime = new DateTime(date.Year, 12, 31); } return (startTime, endTime); } #endregion #region 获取自然半年起止时间 /// <summary> /// 获取自然半年起止时间 /// </summary> /// <param name="date"></param> /// <returns></returns> public static (DateTime startTime, DateTime endTime) GetHalfYearDate(DateTime date) { //默认上半年时间 var startTime = new DateTime(date.Year, 1, 1); var endTime = new DateTime(date.Year, 7, 1).AddDays(-1); //下半年 if (date.Date > endTime) { startTime = endTime.AddDays(1); endTime = startTime.AddYears(1).AddDays(-1); } return (startTime, endTime); } #endregion #region 获取自然年起止时间 /// <summary> /// 获取自然年起止时间 /// </summary> /// <param name="date"></param> /// <returns></returns> public static (DateTime startTime, DateTime endTime) GetYearDate(DateTime date) { //年开始时间 var startTime = new DateTime(date.Year, 1, 1); //年结束时间 var endTime = startTime.AddYears(1).AddDays(-1); return (startTime, endTime); } #endregion #region 自然时间验证 /// <summary> /// 自然时间验证 /// </summary> /// <param name="startTime">任务开始时间</param> /// <param name="endTime">任务截止时间</param> /// <param name="dataStarTime">数据开始时间</param> /// <param name="dataEndTime">数据截止时间</param> /// <param name="planType">计划要求</param> /// <param name="count">数据总数量</param> /// <param name="index">数据下标</param> /// <returns></returns> public static string ValidateNaturalTime(DateTime startTime, DateTime endTime, DateTime dataStarTime, DateTime dataEndTime, PlanType planType, int count, int index) { //错误信息 var message = ""; if (dataEndTime < dataStarTime) { return "存在结束时间小于开始时间数据"; } //若当前数据为唯一数据 if (count == 1 && (dataStarTime != startTime || dataEndTime != endTime)) { return "数据起止时间与任务起止时间不符"; } var start = dataStarTime; //用于记录数据的开始时间 var end = dataEndTime; //用于记录数据的结束时间 switch (planType) { //周 case PlanType.Week: //周开始时间 start = GetWeekDate(dataStarTime).startTime; //周结束时间 end = GetWeekDate(dataStarTime).endTime; break; //月 case PlanType.Month: //月开始时间 start = dataStarTime.GetFirstDayOfMonth(); //月结束时间 end = dataStarTime.GetLastDayOfMonth(); break; //季 case PlanType.Quarter: start = GetQuarterDate(dataStarTime).startTime; end = GetQuarterDate(dataStarTime).endTime; break; //半年 case PlanType.HalfYear: start = GetHalfYearDate(dataStarTime).startTime; end = GetHalfYearDate(dataStarTime).endTime; break; //年 case PlanType.Year: //年开始时间 start = GetYearDate(dataStarTime).startTime; //年结束时间 end = GetYearDate(dataStarTime).endTime; break; } //第一条数据 if (index == 0 && (dataStarTime != startTime || dataEndTime != end)) { message = $"第1{planType.GetDescriptions()}起止时间存在错误"; } //最后一条数据 else if (index == count - 1 && (dataStarTime != start || dataEndTime != endTime)) { message = $"第{index + 1}{planType.GetDescriptions()}起止时间存在错误"; } //中间时间段 else if (index != count - 1 && index != 0 && (dataStarTime != start || dataEndTime != end)) { message = $"第{index + 1}{planType.GetDescriptions()}起止时间存在错误"; } return message; } #endregion }