/// <summary> /// 得到两个日期的指定格式间隔 /// </summary> /// <param name="dt1">日期1</param> /// <param name="dt2">日期2</param> /// <param name="dateformat">间隔格式: y:年 M:月 d:天 h:小时 m:分钟 s:秒</param> /// <returns>间隔long型</returns> public static long GetIntervalOf2DateTime(DateTime dt1, DateTime dt2, string dateformat) { try { long interval = dt1.Ticks - dt2.Ticks; DateTime dt11; DateTime dt22; switch (dateformat) { case "s"://秒 interval /= 10000000; break; case "m"://分钟 interval /= 600000000; break; case "h"://小时 interval /= 36000000000; break; case "d"://天 interval /= 864000000000; break; case "M"://月 dt11 = (dt1.CompareTo(dt2) >= 0) ? dt2 : dt1; dt22 = (dt1.CompareTo(dt2) >= 0) ? dt1 : dt2; interval = -1; while (dt22.CompareTo(dt11) >= 0) { interval++; dt11 = dt11.AddMonths(1); } break; case "y"://年 dt11 = (dt1.CompareTo(dt2) >= 0) ? dt2 : dt1; dt22 = (dt1.CompareTo(dt2) >= 0) ? dt1 : dt2; interval = -1; while (dt22.CompareTo(dt11) >= 0) { interval++; dt11 = dt11.AddMonths(1); } interval /= 12; break; } return interval; } catch { return 0; } }