/// <summary> /// 计算日期间隔 /// </summary> /// <param name="startDate">开始时间</param> /// <param name="endDate">结束时间</param> /// <param name="drf">决定返回值形式的枚举</param> /// <returns>年月日的int数组,具体数组长度与枚举参数drf有关</returns> public static int[] DiffTimes(DateTime startDate, DateTime endDate, DiffResultFormat drf) { try { #region 数据初始化 DateTime max; DateTime min; if (startDate > endDate) { max = startDate; min = endDate; } else { max = endDate; min = startDate; } int tempYear = max.Year; int tempMonth = max.Month; if (max.Month < min.Month) { tempYear--; tempMonth = tempMonth + 12; } int year = tempYear - min.Year; int month = (year == 0 && tempMonth - min.Month == 0) ? 1 : tempMonth - min.Month; #endregion #region 条件计算 switch (drf) { case DiffResultFormat.yy: return(new int[] { year, 0, 0, 0, 0 }); case DiffResultFormat.hh: return(new int[] { year, year * 2 + month, 0, 0, 0 }); case DiffResultFormat.qq: return(new int[] { year, year * 2 + month, year * 4 + month, 0, 0 }); case DiffResultFormat.mm: return(new int[] { year, year * 2 + month, year * 4 + month, year * 12 + month, 0 }); case DiffResultFormat.dd: TimeSpan ts = max - min; return(new int[] { 0, 0, 0, 0, ts.Days }); default: throw new Exception("字符串参数不正确!"); } #endregion } catch (Exception ex) { throw ex; } }
/// <summary> /// 计算日期间隔 /// </summary> /// <param name="d1">要参与计算的其中一个日期字符串 /// <param name="d2">要参与计算的另一个日期字符串 /// <param name="drf">决定返回值形式的枚举 /// <returns>一个代表年月日的int数组,具体数组长度与枚举参数drf有关</returns> public static int[] toResult(string d1, string d2, DiffResultFormat drf) { try { DateTime date1 = DateTime.Parse(d1); DateTime date2 = DateTime.Parse(d2); return(toResult(date1, date2, drf)); } catch { throw new Exception("字符串参数不正确!"); } }
/// <summary> /// 计算日期间隔 /// </summary> /// <param name="d1">要参与计算的其中一个日期字符串</param> /// <param name="d2">要参与计算的另一个日期字符串</param> /// <param name="drf">决定返回值形式的枚举 /// <see cref="Infrastructure.Crosscutting.Declaration.DiffResultFormat"/> /// </param> /// <returns>一个代表年月日的int数组,具体数组长度与枚举参数drf有关</returns> public static int[] ToResult(string d1, string d2, DiffResultFormat drf) { try { DateTime date1 = DateTime.Parse(d1); DateTime date2 = DateTime.Parse(d2); return ToResult(date1, date2, drf); } catch { throw new Exception("字符串参数不正确!"); } }
/// <summary> /// 计算日期间隔 /// </summary> /// <param name="d1">要参与计算的其中一个日期</param> /// <param name="d2">要参与计算的另一个日期</param> /// <param name="drf">决定返回值形式的枚举 /// <see cref="Infrastructure.Crosscutting.Declaration.DiffResultFormat"/> /// </param> /// <returns>一个代表年月日的int数组,具体数组长度与枚举参数drf有关</returns> public static int[] ToResult(DateTime d1, DateTime d2, DiffResultFormat drf) { #region 数据初始化 DateTime max; DateTime min; int year; int month; int tempYear, tempMonth; if (d1 > d2) { max = d1; min = d2; } else { max = d2; min = d1; } tempYear = max.Year; tempMonth = max.Month; if (max.Month < min.Month) { tempYear--; tempMonth = tempMonth + 12; } year = tempYear - min.Year; month = tempMonth - min.Month; #endregion #region 按条件计算 if (drf == DiffResultFormat.dd) { TimeSpan ts = max - min; return new int[] { ts.Days }; } if (drf == DiffResultFormat.mm) { return new int[] { month + year * 12 }; } if (drf == DiffResultFormat.yy) { return new int[] { year }; } return new int[] { year, month }; #endregion }
/// <summary> /// 计算日期间隔 /// </summary> /// <param name="d1">要参与计算的其中一个日期</param> /// <param name="d2">要参与计算的另一个日期</param> /// <param name="drf">决定返回值形式的枚举</param> /// <returns>一个代表年月日的int数组,具体数组长度与枚举参数drf有关</returns> public static int[] ToDiffResult(DateTime d1, DateTime d2, DiffResultFormat drf) { #region 数据初始化 DateTime max; DateTime min; int year; int month; int tempYear, tempMonth; if (d1 > d2) { max = d1; min = d2; } else { max = d2; min = d1; } tempYear = max.Year; tempMonth = max.Month; if (max.Month < min.Month) { tempYear--; tempMonth = tempMonth + 12; } year = tempYear - min.Year; month = tempMonth - min.Month; #endregion #region 条件计算 if (drf == DiffResultFormat.dd) { TimeSpan ts = max - min; return(new int[] { ts.Days }); } if (drf == DiffResultFormat.mm) { return(new int[] { month + year * 12 }); } if (drf == DiffResultFormat.yy) { return(new int[] { year }); } return(new int[] { year, month }); #endregion }
/// <summary> /// 计算日期间隔 /// </summary> /// <param name="d1">要参与计算的其中一个日期 /// <param name="d2">要参与计算的另一个日期 /// <param name="drf">决定返回值形式的枚举 /// <returns>一个代表年月日的int数组,具体数组长度与枚举参数drf有关</returns> public static int[] toResult(DateTime d1, DateTime d2, DiffResultFormat drf) { #region 数据初始化 DateTime max; DateTime min; int year; //年 int month; //月 int day; //天 int hour; //小时 int minute; //分钟 int tempYear, tempMonth, tempDay, tempHour, tempMinute; if (d1 > d2) { max = d1; min = d2; } else { max = d2; min = d1; } tempYear = max.Year; tempMonth = max.Month; tempDay = max.Day; tempHour = max.Hour; tempMinute = max.Minute; if (max.Month < min.Month) { tempYear--; tempMonth = tempMonth + 12; } if (max.Day < min.Day) { tempMonth--; tempDay = tempDay + GetDay(tempYear, tempMonth); } if (max.Hour < min.Hour) { tempDay--; tempHour = tempHour + 24; } if (max.Minute < min.Minute) { tempHour--; tempMinute = tempMinute + 60; } year = tempYear - min.Year; month = tempMonth - min.Month; day = tempDay - min.Day; hour = tempHour - min.Hour; minute = tempMinute - min.Minute; #endregion #region 条件计算 if (drf == DiffResultFormat.dd) { TimeSpan ts = max - min; return(new int[] { ts.Days }); } if (drf == DiffResultFormat.mm) { return(new int[] { month + year * 12 }); } if (drf == DiffResultFormat.yy) { return(new int[] { year }); } if (drf == DiffResultFormat.yymm) { return(new int[] { year, month }); } if (drf == DiffResultFormat.yyMMddHHmm) { return(new int[] { year, month, day, hour, minute }); } return(new int[] { year, month, day, hour, minute }); #endregion }