計算兩個日期間的天數、時數、分數、秒數

DateTime dt1 = new DateTime(2008, 12, 31);  
DateTime dt2 = new 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 ));

//相差秒數(return double type)
Response.Write(Convert.ToString( ts.TotalSeconds ));

沒有留言: