-2

This is now bugging me , i have tried to fix it for the past hour but still no luck!

I hope some one could spot what i'm doing wrong . here is my code:

var maxDays = 30;
 DateTime today = DateTime.Now; //todays date
 DateTime lastAction = '2017-03-07 12:47:58.967';
  double totalDays = (lastAction - today).TotalDays;
  var days = Math.Round(totalDays);
if(days > maxDays)
{
  //never hits this even though days is greater than max days ..i'm so confused 
}

what am i doing wrong?

1future
  • 255
  • 5
  • 21
  • 1
    what did the debugger show you? – pm100 Aug 30 '17 at 15:51
  • 6
    what debugger, it doesn't even compile – Adrian Iftode Aug 30 '17 at 15:51
  • I give the benefit of the doubt and assume that the real code does compile and run – pm100 Aug 30 '17 at 15:51
  • @pm100 `DateTime lastAction = '2017-03-07 12:47:58.967';` won't compile. – mason Aug 30 '17 at 15:52
  • it compiles gents... lastAction is date from a database – 1future Aug 30 '17 at 15:53
  • Did you debug the code? Inspect the value of `days` to see if it matches what you expected? Why didn't you post compilable code in your question? – mason Aug 30 '17 at 15:53
  • 8
    If you're looking for totalDays (elapsed since lastAction) shouldn't it be (today - lastAction). The way you have it, days will be a negative number which will not be greater than maxDays which is a positive number. – Kevin Aug 30 '17 at 15:54
  • THere is no way this compiles. the assignment of lastaction is wrong in many ways. I assume that you are paraphrasing the actual code. Others think that you cannot distinguish between code that wont compile and code that runs but doesnt do what you expect – pm100 Aug 30 '17 at 15:54
  • No, it *does not compile*. This is not valid C#: `DateTime lastAction = '2017-03-07 12:47:58.967';` Instead it should probably be `DateTime lastAction = DateTime.Parse("2017-03-07 12:47:58.967");` or some variant of ParseExact. – mason Aug 30 '17 at 15:55
  • Try DateTime lastAction = new DateTime(2017,03,07, 12,47,58,967); It compiles – JSR Aug 30 '17 at 15:55
  • and I will then loop back to - what does the debugger show? – pm100 Aug 30 '17 at 15:56
  • @1future - As denoted by several other people here, your provided code example does NOT compile as provided. Please provide a [mcve] and the exact error you are receiving. – gravity Aug 30 '17 at 15:56
  • 1
    Learn to use the debugger please. This can be solved in 5 seconds with your own effort by mousing over the variables in the if. – TyCobb Aug 30 '17 at 15:56
  • @Kevin That's the only correct answer, imo. – Trioj Aug 30 '17 at 16:01

5 Answers5

6

Duplicate problem as here: C# Number of days between two dates problem

Timespan.TotalDays can be negative. So in your case it is almost guaranteed that lastAction - today will be a negative number, and so will always be less than 30.

If you only care about the absolute value of days, use Math.Abs otherwise re-arrange so that you are subtracting lastAction from today (today - lastAction). Note that due to rounding, your condition will still not be triggered if there is less than 1 day difference.

2

Is it possible you are subtracting a larger value (today) from a small value (lastaction) which should result in a negative number making days negative?

That and you do need to do an explicit parse on the string to make it a date:

DateTime lastAction = DateTime.Parse("2017-03-07 12:47:58
     .967");
MikeS
  • 1,734
  • 1
  • 9
  • 13
2

Couple of things.

First you cant convert a string to DateTime like that. You should do something like this instead. DateTime lastAction = DateTime.Parse("2017-03-07 12:47:58.967");

Second, Just as @MikeS said, you are subtracting the lastAction from Today, which is resulting in a negative number (in this case its like -173). You should flip that statement. double totalDays = ( today - lastAction).TotalDays;

Your whole section should look something like this.

var maxDays = 30;
DateTime today = DateTime.Now; //todays date
DateTime lastAction = DateTime.Parse("2017-03-07 12:47:58.967");
double totalDays = ( today - lastAction).TotalDays;
var days = Math.Round(totalDays);
if (days > maxDays)
{
    // now this is hit
}
Michael Sharp
  • 496
  • 3
  • 10
0

Thanks for the help. I did something stupid .. I had

double totalDays = (lastAction - today).TotalDays; // returns -176

changed my code to:

 double totalDays = (today - lastAction).TotalDays; //returns 176
1future
  • 255
  • 5
  • 21
  • 1
    IMHO - it would be better to accept one of the answers that had already pointed this out than to create a new answer... – squillman Aug 30 '17 at 16:05
0

Your first problem: You didn't parse the string to DateTime.

DateTime lastAction = Convert.ToDateTime("2017-03-07 12:47:58.967");

Your second problem: You were receiving a negative value, and checking if it's bigger.

var days = (Math.Round(totalDays)) * (-1);

Like this, it should work.

rqnn
  • 348
  • 2
  • 10