DateTimeOffset.Compare(DateTimeOffset, DateTimeOffset) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Compares two DateTimeOffset objects and indicates whether the first is earlier than the second, equal to the second, or later than the second.
public:
static int Compare(DateTimeOffset first, DateTimeOffset second);
public static int Compare (DateTimeOffset first, DateTimeOffset second);
static member Compare : DateTimeOffset * DateTimeOffset -> int
Public Shared Function Compare (first As DateTimeOffset, second As DateTimeOffset) As Integer
- first
- DateTimeOffset
The first object to compare.
- second
- DateTimeOffset
The second object to compare.
A signed integer that indicates whether the value of the first
parameter is earlier than, later than, or the same time as the value of the second
parameter, as the following table shows.
Return value | Meaning |
---|---|
Less than zero | first is earlier than second .
|
Zero | first is equal to second .
|
Greater than zero | first is later than second .
|
The following example illustrates calls to the Compare method to compare DateTimeOffset objects.
using System;
public class CompareTimes
{
private enum TimeComparison
{
Earlier = -1,
Same = 0,
Later = 1
};
public static void Main()
{
DateTimeOffset firstTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0,
new TimeSpan(-7, 0, 0));
DateTimeOffset secondTime = firstTime;
Console.WriteLine("Comparing {0} and {1}: {2}",
firstTime, secondTime,
(TimeComparison) DateTimeOffset.Compare(firstTime, secondTime));
secondTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0,
new TimeSpan(-6, 0, 0));
Console.WriteLine("Comparing {0} and {1}: {2}",
firstTime, secondTime,
(TimeComparison) DateTimeOffset.Compare(firstTime, secondTime));
secondTime = new DateTimeOffset(2007, 9, 1, 8, 45, 0,
new TimeSpan(-5, 0, 0));
Console.WriteLine("Comparing {0} and {1}: {2}",
firstTime, secondTime,
(TimeComparison) DateTimeOffset.Compare(firstTime, secondTime));
// The example displays the following output to the console:
// Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -07:00: Same
// Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -06:00: Later
// Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 8:45:00 AM -05:00: Same
}
}
open System
type TimeComparison =
| Earlier = -1
| Same = 0
| Later = 1
let firstTime = DateTimeOffset(2007, 9, 1, 6, 45, 0, TimeSpan(-7, 0, 0))
let secondTime = firstTime
printfn $"Comparing {firstTime} and {secondTime}: {DateTimeOffset.Compare(firstTime, secondTime) |> enum<TimeComparison>}"
let thirdTime = DateTimeOffset(2007, 9, 1, 6, 45, 0, TimeSpan(-6, 0, 0))
printfn $"Comparing {firstTime} and {thirdTime}: {DateTimeOffset.Compare(firstTime, thirdTime) |> enum<TimeComparison>}"
let fourthTime = DateTimeOffset(2007, 9, 1, 8, 45, 0, TimeSpan(-5, 0, 0))
printfn $"Comparing {firstTime} and {fourthTime}: {DateTimeOffset.Compare(firstTime, fourthTime) |> enum<TimeComparison>}"
// The example displays the following output to the console:
// Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -07:00: Same
// Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -06:00: Later
// Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 8:45:00 AM -05:00: Same
Module CompareTimes
Private Enum TimeComparison As Integer
Earlier = -1
Same = 0
Later = 1
End Enum
Public Sub Main()
Dim firstTime As New DateTimeOffset(#09/01/2007 6:45:00AM#, _
New TimeSpan(-7, 0, 0))
Dim secondTime As DateTimeOffset = firstTime
Console.WriteLine("Comparing {0} and {1}: {2}", _
firstTime, secondTime, _
CType(DateTimeOffset.Compare(firstTime, secondTime), _
TimeComparison))
secondTime = New DateTimeOffset(#09/01/2007 6:45:00AM#, _
New TimeSpan(-6, 0, 0))
Console.WriteLine("Comparing {0} and {1}: {2}", _
firstTime, secondTime, _
CType(DateTimeOffset.Compare(firstTime, secondTime), _
TimeComparison))
secondTime = New DateTimeOffset(#09/01/2007 8:45:00AM#, _
New TimeSpan(-5, 0, 0))
Console.WriteLine("Comparing {0} and {1}: {2}", _
firstTime, secondTime, _
CType(DateTimeOffset.Compare(firstTime, secondTime), _
TimeComparison))
' The example displays the following output to the console:
' Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -07:00: Same
' Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -06:00: Later
' Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 8:45:00 AM -05:00: Same
End Sub
End Module
In performing the comparison, the method converts both the first
and the second
parameters to Coordinated Universal Time (UTC) before it performs the comparison. The method is equivalent to the following:
return DateTime.Compare(first.UtcDateTime, second.UtcDateTime);
DateTime.Compare(first.UtcDateTime, second.UtcDateTime)
Return DateTime.Compare(first.UtcDateTime, second.UtcDateTime)
In other words, the Compare method determines whether two DateTimeOffset objects represent a single point in time. It directly compares neither dates and times nor offsets.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: