In C#, CompareTo is the instance version of comparing dates. a.CompareTo(b) returns a negative number if a is earlier, zero if equal, and a positive number if later.
There are two overloads: one takes a DateTime, the other takes an object so DateTime can implement IComparable. Methods like Array.Sort call it for you.
This is an instance member of System.DateTime. You call it on a DateTime value, such as dt.CompareTo(...). DateTime is a struct, so every value is copied and never changed in place.
Syntax
Call CompareTo on one DateTime and pass the other.
C#
DateTime a = new DateTime(2026, 11, 18);
DateTime b = new DateTime(2026, 11, 19);
int r1 = a.CompareTo(b); // less than zero
int r2 = b.CompareTo(a); // greater than zero
int r3 = a.CompareTo((object)b); // object overload
Call
What it does
a.CompareTo(b)
Compare with another DateTime.
a.CompareTo(obj)
Compare with an object; null counts as smaller.
a.Date.CompareTo(b.Date)
Compare calendar days only, ignoring the time.
Only the sign of the result matters. a.CompareTo(null) returns a positive number. A value that is not a DateTime throws ArgumentException.
Library declarations
C#
public int CompareTo(DateTime value);
public int CompareTo(object? value);
Parameters
Parameter
Description
value (DateTime)
The date to compare with this one.
value (object)
A boxed DateTime, or null. Any other type throws ArgumentException.
Return Value
Return Value
Description
< 0
This instance is earlier than value.
0
This instance equals value.
> 0
This instance is later than value, or value is null.
C# Examples
Example 1 — Earlier, Later, Same
Call CompareTo in both directions and with itself.
C#
using System;
class Program {
static void Main() {
DateTime a = new DateTime(2026, 11, 18);
DateTime b = new DateTime(2026, 11, 19);
Console.WriteLine(Math.Sign(a.CompareTo(b)));
Console.WriteLine(Math.Sign(b.CompareTo(a)));
Console.WriteLine(Math.Sign(a.CompareTo(a)));
}
}
Output
-1
1
0
How It Works
Called on a. The instance on the left is the one being measured.
a is earlier. November 18 comes before November 19, so the sign is -1.
Reverse call.b.CompareTo(a) flips the sign to 1.
Self compare. A date always compares equal to itself, which gives 0.
Example 2 — Null and Wrong Type
Use the object overload with a boxed date, null, and a wrong type.
C#
using System;
class Program {
static void Main() {
DateTime a = new DateTime(2026, 11, 18);
object boxed = new DateTime(2026, 11, 20);
Console.WriteLine(Math.Sign(a.CompareTo(boxed)));
Console.WriteLine(Math.Sign(a.CompareTo(null)));
try {
a.CompareTo("2026-11-20");
} catch (ArgumentException) {
Console.WriteLine("ArgumentException");
}
}
}
Output
-1
1
ArgumentException
How It Works
Boxed DateTime. The object holds November 20, so a is earlier: -1.
Null. Any real value is considered greater than null, so the sign is 1.
Wrong type. A string is not a DateTime, so the method refuses to compare.
Catch it.ArgumentException is caught and its name is printed.
Example 3 — Same Day, Different Time
Compare full values and then only the Date part.
C#
using System;
class Program {
static void Main() {
DateTime morning = new DateTime(2026, 11, 18, 9, 0, 0);
DateTime evening = new DateTime(2026, 11, 18, 17, 0, 0);
Console.WriteLine(Math.Sign(morning.CompareTo(evening)));
Console.WriteLine(Math.Sign(morning.Date.CompareTo(evening.Date)));
}
}
Output
-1
0
How It Works
Same calendar day. Both values are on November 18, at 9:00 and 17:00.
Full compare. 9:00 is earlier than 17:00, so the sign is -1.
Date property.Date sets the time of day to midnight on each side.
Equal days. After removing the time, the two values are the same, so the result is 0.
Frequently Asked Questions
Compare is static and takes two dates. CompareTo is an instance method that compares this date with another.
It returns a positive number because any DateTime is greater than null.
It throws ArgumentException because the object must be a DateTime.
Use a.Date.CompareTo(b.Date). The Date property sets the time to midnight.
Yes. DateTime implements IComparable, so Array.Sort calls CompareTo automatically.