C# DateTime Compare() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

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

ParameterDescription
t1Required. The first DateTime.
t2Required. The second DateTime.

Return Value

Return ValueDescription
< 0t1 is earlier than t2.
0t1 and t2 are the same instant (same ticks).
> 0t1 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)));
    }
}

How It Works

  1. Two dates. a is November 18 and b is December 25.
  2. a before b. The result is negative, and Math.Sign turns it into -1.
  3. Swap the order. The result becomes positive, which prints 1.
  4. 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));
        }
    }
}

How It Works

  1. Unsorted list. The three dates are added in December, January, June order.
  2. Method group. DateTime.Compare already fits the Comparison<DateTime> shape.
  3. Oldest first. Sort calls it again and again and places negatives first.
  4. 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);
    }
}

How It Works

  1. Same clock text. Both values read 10:00 on November 18, 2026.
  2. Different Kind. One is marked Utc, the other Local.
  3. Result is zero. Compare sees equal ticks and ignores the Kind flag.
  4. 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.

Next: CompareTo()

Compare this DateTime to another value and get less than, equal to, or greater than zero with System.DateTime.

CompareTo() 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.

7 people found this page helpful