TimeSpan.Compare(TimeSpan, TimeSpan) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
두 TimeSpan 값을 비교하고 첫 번째 값이 두 번째 값보다 짧거나 같거나 긴지 여부를 나타내는 정수를 반환합니다.
public:
static int Compare(TimeSpan t1, TimeSpan t2);
public static int Compare(TimeSpan t1, TimeSpan t2);
static member Compare : TimeSpan * TimeSpan -> int
Public Shared Function Compare (t1 As TimeSpan, t2 As TimeSpan) As Integer
매개 변수
- t1
- TimeSpan
비교할 첫 번째 시간 간격입니다.
- t2
- TimeSpan
비교할 두 번째 시간 간격입니다.
반품
다음 값 중 하나입니다.
| 값 | 설명 |
|---|---|
| -1 |
t1 가 .보다 t2짧습니다.
|
| 0 |
t1가 같음 t2
|
| 1 |
t1 가 .보다 t2깁니다.
|
예제
다음 예제에서는 이 메서드를 Compare 사용하여 값이 2시간 간격인 TimeSpan 개체와 여러 TimeSpan 개체를 비교합니다.
// Define a time interval equal to two hours.
TimeSpan baseInterval = new TimeSpan( 2, 0, 0);
// Define an array of time intervals to compare with
// the base interval.
TimeSpan[] spans = {
TimeSpan.FromSeconds(-2.5),
TimeSpan.FromMinutes(20),
TimeSpan.FromHours(1),
TimeSpan.FromMinutes(90),
baseInterval,
TimeSpan.FromDays(.5),
TimeSpan.FromDays(1)
};
// Compare the time intervals.
foreach (var span in spans) {
int result = TimeSpan.Compare(baseInterval, span);
Console.WriteLine("{0} {1} {2} (Compare returns {3})",
baseInterval,
result == 1 ? ">" : result == 0 ? "=" : "<",
span, result);
}
// The example displays the following output:
// 02:00:00 > -00:00:02.5000000 (Compare returns 1)
// 02:00:00 > 00:20:00 (Compare returns 1)
// 02:00:00 > 01:00:00 (Compare returns 1)
// 02:00:00 > 01:30:00 (Compare returns 1)
// 02:00:00 = 02:00:00 (Compare returns 0)
// 02:00:00 < 12:00:00 (Compare returns -1)
// 02:00:00 < 1.00:00:00 (Compare returns -1)
open System
// Define a time interval equal to two hours.
let baseInterval = TimeSpan(2, 0, 0)
// Define a list of time intervals to compare with
// the base interval.
let spans =
[ TimeSpan.FromSeconds -2.5
TimeSpan.FromMinutes 20
TimeSpan.FromHours 1
TimeSpan.FromMinutes 90
baseInterval
TimeSpan.FromDays 0.5
TimeSpan.FromDays 1 ]
// Compare the time intervals.
for span in spans do
let result = TimeSpan.Compare(baseInterval, span)
printfn $"""{baseInterval} {if result = 1 then ">" elif result = 0 then "=" else "<"} {span} (Compare returns {result})"""
// The example displays the following output:
// 02:00:00 > -00:00:02.5000000 (Compare returns 1)
// 02:00:00 > 00:20:00 (Compare returns 1)
// 02:00:00 > 01:00:00 (Compare returns 1)
// 02:00:00 > 01:30:00 (Compare returns 1)
// 02:00:00 = 02:00:00 (Compare returns 0)
// 02:00:00 < 12:00:00 (Compare returns -1)
// 02:00:00 < 1.00:00:00 (Compare returns -1)
Public Module Example
Public Sub Main()
' Define a time interval equal to 2 hours.
Dim baseInterval As New TimeSpan( 2, 0, 0)
' Define an array of time intervals to compare with
' the base interval.
Dim spans() As TimeSpan = { TimeSpan.FromSeconds(-2.5),
TimeSpan.FromMinutes(20),
TimeSpan.FromHours(1),
TimeSpan.FromMinutes(90),
baseInterval,
TimeSpan.FromDays(.5),
TimeSpan.FromDays(1) }
' Compare the time intervals.
For Each span In spans
Dim result As Integer = TimeSpan.Compare(baseInterval, span)
Console.WriteLine("{0} {1} {2} (Compare returns {3})",
baseInterval,
If(result = 1, ">", If(result = 0, "=", "<")),
span, result)
Next
End Sub
End Module
' The example displays the following output:
' 02:00:00 > -00:00:02.5000000 (Compare return
' 02:00:00 > 00:20:00 (Compare returns 1
' 02:00:00 > 01:00:00 (Compare returns 1
' 02:00:00 > 01:30:00 (Compare returns 1
' 02:00:00 = 02:00:00 (Compare returns 0
' 02:00:00 < 12:00:00 (Compare returns -1
' 02:00:00 < 1.00:00:00 (Compare returns -1