Subtract DateOnly in C#
Asked Answered
A

4

47

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'
Adolphadolphe answered 19/5, 2022 at 13:41 Comment(6)
Does this answer your question? Calculate difference between two dates (number of days)?Foreshow
You can use extension .ToDateTime(bool timeOnly) on your DateOnly so you can have behaviour of DateTimeNearly
@Amit Verma Thanks for answer, but not really. I need to use DateOnly structure instead of DateTime, which is used in link.Adolphadolphe
Since you aren't asking for a solution i figure my answer will be sufficient?Ranit
When it was introduced: "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.Selfrestraint
@rbdeebk thanks for answer! I'm more interested in the reasonAdolphadolphe
Y
83

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;
Yost answered 19/5, 2022 at 13:57 Comment(2)
Hi, If we suppose, one of them is nullable how can I do that?Cf
@L.Kvri Use .Value or cast to DateOnly, along with any necessary null checks, as with other nullable value types.Yost
K
10

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;
Kawasaki answered 19/5, 2022 at 13:57 Comment(0)
L
2

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.

Led answered 3/8, 2022 at 17:46 Comment(2)
The answer is 1 :-)Adust
It's really simple, June 1 minus June 1 is obviously zero. By forcing us to use DayNumber exactly nothing is made any less error-prone, just a bit more typing to do...Yawn
E
-1

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.

Electuary answered 19/5, 2022 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.