Subtract DateOnly in C#

Conceptually DateOnly represents an entire day, not midnight or any other specific time on a given day, such that subtracting one DateOnly from another cannot logically return a TimeSpan as with DateTime‘s subtraction operator.

If you want to perform arithmetic on DateOnlys, you need to be explicit about the desired unit.

DateOnly has a DayNumber property, that returns the number of whole days since 01/01/0001, so if you want to determine the number of whole days between 2 DateOnly values, you can do the following:

var d = new DateOnly(2000, 01, 01);
var e = new DateOnly(1999, 01, 01);

var daysDifference = d.DayNumber - e.DayNumber;

Leave a Comment