---
# System prepended metadata

title: 比較兩個時間差 ( 只比較到分鐘 )
tags: [C#]

---

# 比較兩個時間差 ( 只比較到分鐘 )
###### tags: `C#`

```
// 比較兩個時間差 ( 只比較到分鐘 )
var timeA = new DateTime(2021, 5, 25, 12, 05, 10);
var timeB = new DateTime(2021, 5, 25, 12, 05, 59);
// TotalMinutes 用 Math.Floor 無條件捨去，就可以去掉因為秒數有的小數點
var timeA_timeOfDayUntilMinutes = Math.Floor(timeA.TimeOfDay.TotalMinutes);
var timeB_timeOfDayUntilMinutes = Math.Floor(timeB.TimeOfDay.TotalMinutes);

Console.WriteLine(timeA.Date);				
Console.WriteLine(timeB.Date);		
Console.WriteLine(timeA_timeOfDayUntilMinutes);
Console.WriteLine(timeB_timeOfDayUntilMinutes);

// 別忘了比較到 Date !
Console.WriteLine(timeA.Date == timeB.Date && timeA_timeOfDayUntilMinutes == timeB_timeOfDayUntilMinutes);
Console.WriteLine(timeA.Date > timeB.Date || timeA_timeOfDayUntilMinutes > timeB_timeOfDayUntilMinutes);
```

<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/mWk89F" frameborder="0"></iframe>