摘要:[C#]2個DateTime日期間的相差天數.小時數.分鐘數.秒數
DateTime dt1 = new DateTime(2008, 12, 31);
DateTime dt2 = new DateTime();
dt2 = DateTime.Now;
TimeSpan ts = new TimeSpan(dt2.Ticks - dt1.Ticks);
//相差天數(未滿一天捨去,return int type)
Response.Write(Convert.ToString( ts.Days ));
//相差天數(未滿一天亦計入,return double type)
Response.Write(Convert.ToString( ts.TotalDays ));
//相差小時數(return double type)
Response.Write(Convert.ToString( ts.TotalHours ));
//相差秒數(return double type)
Response.Write(Convert.ToString( ts.TotalMinutes ));
DateTime dt2 = new DateTime();
dt2 = DateTime.Now;
TimeSpan ts = new TimeSpan(dt2.Ticks - dt1.Ticks);
//相差天數(未滿一天捨去,return int type)
Response.Write(Convert.ToString( ts.Days ));
//相差天數(未滿一天亦計入,return double type)
Response.Write(Convert.ToString( ts.TotalDays ));
//相差小時數(return double type)
Response.Write(Convert.ToString( ts.TotalHours ));
//相差秒數(return double type)
Response.Write(Convert.ToString( ts.TotalMinutes ));
參考資料: MSDN TimeSpan