-1

My task is to get quantity of Days when I subtract the Current Date from a fixed date.

<script language="C#" runat="server">
    System.DateTime thisDay = DateTime.Today;
    System.DateTime dateCountFrom = new DateTime(2016, 6, 1, 0, 00, 00);
</script>

So I have 2 dates and everything is working perfectly. But how can I subtract one from the other?

 (dateCountFrom-thisDay).totaldayscount;

causes an error, tried other ways - same result.

I succeeded only by doing this:

<%= ((DateTime.Now-(new DateTime(2016,6,1))) *-1).TotalDays %>

But I need to eval result and multiply by -1.

Any ideas how I can do this?

Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26
Cove
  • 655
  • 3
  • 7
  • 17
  • Possible duplicate of [Calculate difference between two dates (number of days)?](http://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days) – raidensan Apr 21 '16 at 09:50
  • Store the result of `(DateTime.Today-new DateTime(2016, 6, 1).TotalDays` in a variable and multiply that with -1 – Tim Schmelter Apr 21 '16 at 09:52
  • "makes an error", you need to tell us which error you get. `totaldayscount` is not a member of `TimeSpan` however so most likely that is what the compiler is complaining about. To multiply with `-1`, do that afterwards: `- (DateTime.Now - new DateTime(2016, 6, 1)).TotalDays`, *or*, since `-(a-b) = b-a` you can just do: `new DateTime(2016, 6, 1) - DateTime.Now`. – Lasse V. Karlsen Apr 21 '16 at 09:57
  • Tim Schmelter, it's works and it's a pity that you added as comment but not as answer. If you do I 'll accept it. <%= (((DateTime.Today-new DateTime(2016, 6, 1)).TotalDays)*-1)%> But i still don't understand how to make this calculation inside – Cove Apr 21 '16 at 10:50

2 Answers2

0

You should use the DateTime.Subtract method, this will return a TimeSpan variable containing the difference in time between the dates

TimeSpan diff = dateCountFrom.Subtract(DateTime.Now);
diff = TimeSpan.FromTicks(diff.Ticks * -1);

Instead of this though, if you want it multiplied by -1, you can just do the opposite sum

TimeSpan diff = DateTime.Now.Subtract(dateCountFrom);
Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26
0

(thisDay - dateCountFrom).TotalDays and (dateCountFrom-thisDay).TotalDays are both perfectly legal and won't result in an error. The only difference is that one result will be negative and the other positive.

If you only need the number of days between the two dates, regardless of which one is later, you can use

int days = Math.Abs((thisDay - dateCountFrom).TotalDays)

If you know, one of the dates will always be later then the other one, (for instance dateCountFrom will always be in the future) use this as minuend (ie the date, which is subtracted from) and the other one as subtrahend (ie the date, that is substracted). Then the number of days will also be always positive.

derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • As for me I get CS0117: 'System.Math' does not contain a definition for 'abs' after inserting – Cove Apr 21 '16 at 11:44