In C#, DateTime.Compare(t1, t2) looks at two dates and returns an int. A negative number means t1 is earlier, zero means they are the same instant, and a positive number means t1 is later.
Because the method is static and matches the signature of Comparison<DateTime>, you can pass DateTime.Compare directly to List<DateTime>.Sort.
This is a static member of System.DateTime. You call it with the type name, such as DateTime.Compare(...). DateTime holds a date and time of day with a Kind of Utc, Local, or Unspecified.
Syntax
Call it with the type name and give two DateTime values.
C#
DateTime a = new DateTime(2026, 11, 18);
DateTime b = new DateTime(2026, 12, 25);
int r1 = DateTime.Compare(a, b); // less than zero
int r2 = DateTime.Compare(b, a); // greater than zero
int r3 = DateTime.Compare(a, a); // zero
Call
What it does
DateTime.Compare(a, b)
Negative when a is earlier than b.
DateTime.Compare(b, a)
Positive when the first date is later.
list.Sort(DateTime.Compare)
Sort a list of dates from oldest to newest.
Do not test for exactly -1 or 1. Only the sign is guaranteed. Use Math.Sign(result) or compare with < 0, == 0, and > 0.
Library declarations
C#
public static int Compare(DateTime t1, DateTime t2);
Parameters
Parameter
Description
t1
Required. The first DateTime.
t2
Required. The second DateTime.
Return Value
Return Value
Description
< 0
t1 is earlier than t2.
0
t1 and t2 are the same instant (same ticks).
> 0
t1 is later than t2.
C# Examples
Example 1 — Earlier, Later, Equal
Compare two fixed dates in both orders and with itself.
C#
using System;
class Program {
static void Main() {
DateTime a = new DateTime(2026, 11, 18);
DateTime b = new DateTime(2026, 12, 25);
Console.WriteLine(Math.Sign(DateTime.Compare(a, b)));
Console.WriteLine(Math.Sign(DateTime.Compare(b, a)));
Console.WriteLine(Math.Sign(DateTime.Compare(a, a)));
}
}
Output
-1
1
0
How It Works
Two dates.a is November 18 and b is December 25.
a before b. The result is negative, and Math.Sign turns it into -1.
Swap the order. The result becomes positive, which prints 1.
Same date. Comparing a with itself gives 0.
Example 2 — Sort with DateTime.Compare
Pass the static method to List.Sort to order dates.
C#
using System;
using System.Collections.Generic;
using System.Globalization;
class Program {
static void Main() {
var dates = new List<DateTime> {
new DateTime(2026, 12, 1),
new DateTime(2026, 1, 15),
new DateTime(2026, 6, 30)
};
dates.Sort(DateTime.Compare);
foreach (DateTime d in dates) {
Console.WriteLine(d.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
}
}
}
Output
2026-01-15
2026-06-30
2026-12-01
How It Works
Unsorted list. The three dates are added in December, January, June order.
Method group.DateTime.Compare already fits the Comparison<DateTime> shape.
Oldest first.Sort calls it again and again and places negatives first.
Loop prints. The result reads January, June, December.
Example 3 — Same Ticks, Different Kind
Show that Compare does not look at DateTimeKind.
C#
using System;
class Program {
static void Main() {
DateTime utc = new DateTime(2026, 11, 18, 10, 0, 0, DateTimeKind.Utc);
DateTime local = new DateTime(2026, 11, 18, 10, 0, 0, DateTimeKind.Local);
Console.WriteLine(Math.Sign(DateTime.Compare(utc, local)));
Console.WriteLine(utc.Kind);
Console.WriteLine(local.Kind);
}
}
Output
0
Utc
Local
How It Works
Same clock text. Both values read 10:00 on November 18, 2026.
Different Kind. One is marked Utc, the other Local.
Result is zero.Compare sees equal ticks and ignores the Kind flag.
Be careful. The two values may be hours apart in real time, so convert first when it matters.
Frequently Asked Questions
It returns less than zero, zero, or greater than zero. Do not rely on exact values like -1 or 1.
Yes. Call it as DateTime.Compare(a, b). The instance version is CompareTo.
No. It compares ticks only. Convert both values to UTC first when kinds differ.
Yes. list.Sort(DateTime.Compare) sorts from oldest to newest.
Both compare ticks. Operators like < and == are shorter for simple checks.