DateTime supports the normal comparison operators, so the simplest comparison is the one you would write for numbers:
var deadline = new DateTime(2024, 3, 15);
var submitted = new DateTime(2024, 3, 15, 16, 30, 0);
Console.WriteLine(submitted > deadline); // True
Console.WriteLine(submitted.Date > deadline.Date); // False
Console.WriteLine(submitted.Date == deadline.Date); // True
The first line is the trap most people hit. new DateTime(2024, 3, 15) means midnight at the start of that day, so anything submitted during March 15 is “later” than the deadline. When only the calendar day matters, compare the .Date properties, which set the time to midnight on both sides.
DateTime.Compare and CompareTo
DateTime.Compare(a, b) returns a negative number if a is earlier, zero if they are equal, and a positive number if a is later. a.CompareTo(b) does the same. Test the sign, not specific values like -1:
var first = new DateTime(2009, 5, 5);
var second = new DateTime(2024, 3, 15);
string relation = DateTime.Compare(first, second) switch
{
< 0 => "earlier than",
0 => "the same as",
_ => "later than"
};
Console.WriteLine($"{first:yyyy-MM-dd} is {relation} {second:yyyy-MM-dd}");
// 2009-05-05 is earlier than 2024-03-15
The operators read better in an if. Compare and CompareTo are what sorting needs: List<DateTime>.Sort() and LINQ’s OrderBy use CompareTo under the hood.
Use DateOnly when there is no time
Since .NET 6, DateOnly represents a calendar date without a time of day, so the midnight problem cannot happen:
var dueDate = new DateOnly(2024, 3, 15);
var today = DateOnly.FromDateTime(DateTime.Today);
if (today > dueDate)
{
Console.WriteLine("Overdue");
}
Birthdays, due dates and holidays are dates, not points in time. Modelling them as DateOnly also stops time zone conversions from moving them to the previous day.
How far apart are two dates
Subtracting one DateTime from another gives a TimeSpan:
TimeSpan gap = second - first;
Console.WriteLine(gap.Days); // 5428
Console.WriteLine(gap.TotalHours); // 130272
Days is the whole-day part, while TotalDays, TotalHours and the other Total properties return the full span as a double. There is no TotalMonths or TotalYears, because months and years differ in length. To find out whether a date is more than a month ago, compare with AddMonths: if (date < DateTime.Today.AddMonths(-1)).
When two timestamps come from different sources (two servers, or a value that went through a database that stores milliseconds only), exact equality often fails by a few ticks. Compare with a tolerance instead:
bool roughlyEqual = (a - b).Duration() < TimeSpan.FromSeconds(1);
Duration() returns the absolute value of the span, so the order of a and b does not matter.
The time zone trap
DateTime comparisons look only at the ticks. They ignore DateTime.Kind, so a UTC value and a local value are compared as if both were in the same time zone:
var utc = new DateTime(2024, 3, 15, 12, 0, 0, DateTimeKind.Utc);
var local = utc.ToLocalTime();
Console.WriteLine(utc == local); // False, unless the machine's time zone is UTC
Both variables describe the same instant, and the comparison still says they differ. Either convert both sides with ToUniversalTime() before comparing, or use DateTimeOffset, which compares the actual instant:
var inLondon = new DateTimeOffset(2024, 3, 15, 12, 0, 0, TimeSpan.Zero);
var inAthens = new DateTimeOffset(2024, 3, 15, 14, 0, 0, TimeSpan.FromHours(2));
Console.WriteLine(inLondon == inAthens); // True
Console.WriteLine(inLondon.EqualsExact(inAthens)); // False
EqualsExact also requires the same offset, which is rarely what you want. For timestamps stored or sent between systems, DateTimeOffset (or DateTime in UTC throughout) saves a lot of trouble.
Parse with an explicit format
The original version of this example compared against DateTime.Parse("05/05/2009"). Whether that string means May 5 or, say, "06/05/2009" means June 5 or May 6 depends on the culture of the machine. If you must parse a date before comparing it, state the format:
using System.Globalization;
var parsed = DateTime.ParseExact("2009-05-05", "yyyy-MM-dd", CultureInfo.InvariantCulture);