/// <summary> /// 獲取時間差 /// </summary> /// <param name="dtOld">要減的時間</param> /// <param name="dtNow">大的時間</param> /// <returns></returns> public static string GetDateDiff(DateTime dtOld,DateTime dtNow) { TimeSpan tsOld = new TimeSpan(dtOld.Ticks); TimeSpan tsNow= new TimeSpan(dtNow .Ticks ); TimeSpan datediff = tsOld.Subtract(tsNow).Duration(); if (Convert.ToInt32(datediff.Days) <=0) { if (Convert.ToInt32(datediff.Hours) <= 0) { if (Convert.ToInt32(datediff.Minutes) > 0) { return datediff.Minutes.ToString() + "分鐘前"; } else { return ""; } } else { return datediff.Hours .ToString() + "小時前"; } } else { if (Convert.ToInt32(datediff.Days) < 365) { return datediff.Days.ToString() + "天前"; } else { int year = Convert.ToInt32(datediff.Days) / 365; return year .ToString ()+ "年前"; } } }