1.  /// <summary>   
  •     /// 计算日期间隔   
  •     /// </summary>   
  •     /// <param name="d1">要参与计算的其中一个日期</param>   
  •     /// <param name="d2">要参与计算的另一个日期</param>   
  •     /// <param name="drf">决定返回值形式的枚举</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>   
  • public enum diffResultFormat   
  • {   
  •     /// <summary>   
  •     /// 年数和月数   
  •     /// </summary>   
  •     yymm,   
  •     /// <summary>   
  •     /// 年数   
  •     /// </summary>   
  •     yy,   
  •     /// <summary>   
  •     /// 月数   
  •     /// </summary>   
  •     mm,   
  •     /// <summary>   
  •     /// 天数   
  •     /// </summary>   
  •     dd,   
  • }   
  •   
  • 下面我们将使用这个类来计算日期间隔:    
  •   
  •         string str1 = "2007-12-31";   
  •         string str2 = "2009-6-1";    
  •   
  •         int[] kk = dateTimeDiff.toResult(str1, str2, diffResultFormat.mm);   
  •         Console.WriteLine(string.Format("间隔:{0}个月", kk[0]));   
  •         //结果显示为:间隔:18个月    
  •   
  •         DateTime date1 = DateTime.Parse(str1);   
  •         DateTime date2 = DateTime.Parse(str2);    
  •   
  •         int[] kk2 = dateTimeDiff.toResult(date1, date2, diffResultFormat.yymm);   
  •         Console.WriteLine(string.Format("间隔:{0}年{1}个月", kk2[0], kk2[1]));   
  •         //结果显示为:间隔:1年6个月    
  •   
  • 也可以用这个类来计算时间间隔:    
  •   
  •         string str3 = "2009-5-31 1:55:24";   
  •         string str4 = "2009-6-1";    
  •   
  •         int kk3 =dateTimeDiff.toResult(str3, str4).Hours;   
  •         Console.WriteLine(string.Format("间隔:{0}个小时", kk3));   
  •         //结果显示为:间隔:22个小时