C# 时间间隔与时间相加
一、时间间隔
/// <summary> 时间间隔 </summary>
/// <param name="DateTime1">第一个日期和时间</param>
/// <param name="DateTime2">第二个日期和时间</param>
/// <returns>同一天的相隔的分钟的整数部分</returns>
private int DateDiff(DateTime DateTime1, DateTime DateTime2)
{
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() + "秒";
return Convert.ToInt32(ts.TotalMinutes);
}
二、与当前时间差几秒
DateTime dt1 = DateTime.Now;
DateTime dt2=DateTime.Now;
TimeSpan ts = dt1.Subtract(dt2).Duration();
Double douLen = ts.TotalSeconds;
三、时间相加
/// <summary>时间相加</summary>
/// <param name="DateTime1"></param>
/// <param name="DateTime2"></param>
/// <returns>时间和</returns>
private DateTime DateSum(DateTime DateTime1,DateTime DateTime2)
{
DateTime1 = DateTime1.AddHours(DateTime2.Hour);
DateTime1=DateTime1.AddMinutes(DateTime2.Minute);
DateTime1=DateTime1.AddSeconds(DateTime2.Second);
return DateTime1;
}