using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IStation.Untity { /// /// 时间辅助类 /// public static class DateTimeHelper { /// /// 获取两个时间的TimeSpan /// /// /// /// public static TimeSpan CalcuDiff(DateTime d1, DateTime d2) { TimeSpan ts; if (d1 > d2) { ts = d1 - d2; } else { ts = d2 - d1; } return ts; } /// /// 获取最大时间 /// /// /// public static DateTime GetMax(IEnumerable list) { if (list == null || list.Count() < 1) return DateTime.MaxValue; return list.Max(); } /// /// 获取最大时间 /// public static DateTime GetMax(DateTime dt1, DateTime dt2) { if (dt1 < dt2) return dt2; return dt1; } /// /// 获取最小时间 /// /// /// public static DateTime GetMin(IEnumerable list) { if (list == null || list.Count() < 1) return DateTime.MinValue; return list.Min(); } /// /// 获取最小时间 /// public static DateTime GetMin(DateTime dt1, DateTime dt2) { if (dt1 < dt2) return dt1; return dt2; } /// /// 计算相差年数 /// /// /// /// public static int CalcuDiffYear(DateTime d1, DateTime d2) { return Math.Abs(d1.Year - d2.Year); } /// /// 计算相差月份 /// /// /// /// public static int CalcuDiffMonth(DateTime d1, DateTime d2) { var max = GetMax(new List() { d1, d2 }); var min = GetMin(new List() { d1, d2 }); var year = max.Year - min.Year; var month = max.Month - min.Month; if (month < 0) { year -= 1; month += 12; } return year * 12 + month; } } }