c#计算时间差
/// <summary>
/// 计算两个日期的时间间隔
/// </summary>
/// <param name="DateTime1">第一个日期和时间</param>
/// <param name="DateTime2">第二个日期和时间</param>
/// <returns></returns>
public static 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();
string days = ts.Days.ToString();
string hours = ts.Hours.ToString();
string minutes = ts.Minutes.ToString();
string seconds = ts.Seconds.ToString();
if (days != "0")
{
dateDiff += "" + ts.Days.ToString() + "天";
}
if (hours != "0")
{
dateDiff += "" + ts.Hours.ToString() + "小时";
}
if (minutes != "0")
{
dateDiff += "" + ts.Minutes.ToString() + "分钟";
}
if (seconds != "0")
{
dateDiff += "" + ts.Seconds.ToString() + "秒";
}
//dateDiff = ts.Days.ToString() + "天"
//+ ts.Hours.ToString() + "小时"
//+ ts.Minutes.ToString() + "分钟"
//+ ts.Seconds.ToString() + "秒";
return dateDiff;
}