C#计算时间差值

时间:2021-01-03 00:56:14
 /// <summary>
/// 计算时间差值
/// </summary>
/// <param name="DateTime1"></param>
/// <param name="DateTime2"></param>
/// <returns></returns>
private string DateDiff(DateTime DateTime1, DateTime DateTime2)
{
string dateDiff = null;
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
//dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
dateDiff = ts.Days.ToString() + "-" + ts.Hours.ToString();
float nYear = ((float)ts.Days) / ;
//return nYear.ToString();
return dateDiff;
} 调用
DateTime dt1 = new DateTime();
DateTime dt2 = new DateTime();
dt1 = DateTime.Now;
dt2 = DateTime.Parse("2014-1-2 9:00:00");
MessageBox.Show(DateDiff(dt1,dt2));