將兩個DateTime物件相減,取得TimeSpan,再取出時間差異。
DateTime date1 = new DateTime(2016, 10, 15, 8, 0, 0);
DateTime date2 = new DateTime(2016, 10, 16, 9, 0, 0);
TimeSpan ts = date2 - date1;
Console.WriteLine("日差異(int):" + ts.Days); //1
Console.WriteLine("小時差異(int):" + ts.Hours); //1
Console.WriteLine("分鐘差異(int):" + ts.Minutes); //0
Console.WriteLine("秒差異(int):" + ts.Seconds); //0
Console.WriteLine("間距日數(double):" + ts.TotalDays); //1.04166
Console.WriteLine("間距小時數(double):" + ts.TotalHours); //25
Console.WriteLine("間距分鐘數(double):" + ts.TotalMinutes); //1500
Console.WriteLine("間距秒數(double):" + ts.TotalSeconds); //90000
Console.WriteLine("刻度數(long):" + ts.Ticks); //900000000000
沒有留言:
張貼留言