30

In C# I can't use subtraction with DateOnly variables, unlike DateTime. Is there any explanation?

  var a = new DateTime(2000, 01, 01);
  var b = new DateTime(1999, 01, 01);

  //var c = a.Subtract(b);
  var c = a - b;

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

  var f = d - e; // Error - Operator '-' cannot be applied to operands of type 'DateOnly' and 'DateOnly'
Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
Silenus
  • 667
  • 1
  • 7
  • 16
  • Does this answer your question? [Calculate difference between two dates (number of days)?](https://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days) – Amit Verma May 19 '22 at 13:46
  • You can use extension .ToDateTime(bool timeOnly) on your DateOnly so you can have behaviour of DateTime – Kęstutis Ramulionis May 19 '22 at 13:49
  • 1
    @Amit Verma Thanks for answer, but not really. I need to use DateOnly structure instead of DateTime, which is used in link. – Silenus May 19 '22 at 13:50
  • Since you aren't asking for a solution i figure my answer will be sufficient? – Roe May 19 '22 at 13:52
  • 2
    When it was [introduced](https://devblogs.microsoft.com/dotnet/date-time-and-time-zone-enhancements-in-net-6/): "a DateOnly represents the entire date (from the start of the day through the end of the day)". Subtracting one of those from another would be problematic because there are 3 possible answers depending on what inclusivity you would want to consider for the start and end points. – Damien_The_Unbeliever May 19 '22 at 13:52
  • @rbdeebk thanks for answer! I'm more interested in the reason – Silenus May 19 '22 at 14:01

5 Answers5

57

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;
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
6

You can use DayNumber property to do the subtraction, f will hold the number of days between d and e.

var f = d.DayNumber - e.DayNumber;
Scott Mildenberger
  • 1,456
  • 8
  • 17
3

Consider this: June 2, 2022 - June 1, 2022 = ? Is the answer 1 or is the answer 2? It encompasses 2 full days, but the difference is 1. By forcing us to use DayNumber, there is only one possible answer.

0

I don't know an exact reason as to why they haven't made it possible. But if you look at the documententation DateOnly it doesn't contain the operator addition and subtraction. DateTime does.

Roe
  • 633
  • 2
  • 14
0

To answer your question why - DateOnly in operators section it just doesn't have subtraction implemented while DateTime does have one. What you can do is create your own extension similar to this one.