C# DateTime CompareTo() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

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
CallWhat 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

ParameterDescription
value (DateTime)The date to compare with this one.
value (object)A boxed DateTime, or null. Any other type throws ArgumentException.

Return Value

Return ValueDescription
< 0This instance is earlier than value.
0This instance equals value.
> 0This 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)));
    }
}

How It Works

  1. Called on a. The instance on the left is the one being measured.
  2. a is earlier. November 18 comes before November 19, so the sign is -1.
  3. Reverse call. b.CompareTo(a) flips the sign to 1.
  4. 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");
        }
    }
}

How It Works

  1. Boxed DateTime. The object holds November 20, so a is earlier: -1.
  2. Null. Any real value is considered greater than null, so the sign is 1.
  3. Wrong type. A string is not a DateTime, so the method refuses to compare.
  4. 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)));
    }
}

How It Works

  1. Same calendar day. Both values are on November 18, at 9:00 and 17:00.
  2. Full compare. 9:00 is earlier than 17:00, so the sign is -1.
  3. Date property. Date sets the time of day to midnight on each side.
  4. 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.

Next: DaysInMonth()

Get the number of days in a month for a given year (28, 29, 30, or 31) with System.DateTime.

DaysInMonth() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful