1 #region 时间差 d返回天数,h返回小时,m返回分钟,s返回秒 2 public static int DateDiff(DateTime DateTime1, DateTime DateTime2, string strCase) 3 { 4 string dateDiff = null; 5 try 6 { 7 TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); 8 TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); 9 TimeSpan ts = ts1.Subtract(ts2).Duration(); 10 switch (strCase) 11 { 12 case "d": dateDiff = ts.Days.ToString(); 13 break; 14 case "h": dateDiff = ts.Hours.ToString(); 15 break; 16 case "m": dateDiff = ts.Minutes.ToString(); 17 break; 18 case "s": dateDiff = ts.Seconds.ToString(); 19 break; 20 } 21 } 22 catch 23 { 24 } 25 return Int32.Parse(dateDiff); 26 } 27 #endregion
使用方法:
DateDiff(DateTime.Now, time, "d");//差的天数