Search code examples
c#.net

DateTime Subtraction Issue


Why is the difference between these two dates returned as 0 and not 1?

MessageBox.Show($"{(DateTime.Now.AddDays(1)-DateTime.Now).Days}");

Solution

  • As in the Comment already stated:

    because everytime you call DateTime.Now it gets the current state. Now you call it 2 times leading to a sliightly different timestamp. This means the second DateTime.Now).Days has something like 0.000000001 later calltime resulting in your equasion somewhere like 23:59:59.99999 thus resulting not in a full 24h equals... 0 days