1

I have a small problem with the code below, the 'days' variable always seems to be 0 no matter how far apart the days are.

Can you see anything obviously wrong?

        System.TimeSpan span = dates[0] - dates[1]; // e.g. 12/04/2010 11:44:08 and 18/05/2010 11:52:19
        int days = (int)span.TotalDays;

        if (days > 10) //days always seems to be 0
        {
            throw new Exception("Over 10 days");
        }

Thanks

4
  • What does a ToString() on the span say? Commented May 18, 2010 at 11:02
  • Eek exception when input values are not correct. You should return the function with a boolean if the function was successful done. Commented May 18, 2010 at 14:24
  • @RvdK, Some time has passed since you wrote that comment, but you are totally wrong here. You really should NOT litter C# code with return value error checking. That's what exceptions are for. Without exceptions, you can't chain method calls etc. If you really would like to stress input validation here, Jamie should throw ArgumentExceptions for values that are not acceptable. Commented May 18, 2015 at 15:28
  • @JonasW: indeed long time ago. I would indeed use the argument exception. Back then I did not know better... Commented May 19, 2015 at 5:24

5 Answers 5

6

As you are subtracting the later date from the earlier date, according to your comments, TotalDays will be negative. In your example, -36.

Therefore a comparison of (days > 10) will fail. You should use

int days = Math.Abs((int)span.TotalDays);

Assuming you haven't set date[0] equal to date[1], there is no reason why TotalDays will be returning zero for the sample dates you have in your comments.

Sign up to request clarification or add additional context in comments.

Comments

4

The total days should be negative but in any case not zero, cause you substract the earlier date from the later date. It seems dates[0] and dates[1] are not containing what you think.

1 Comment

TimeSpans can be negative, so it wouldn't be exactly 0.
1

I just tested this:

DateTime date1 = new DateTime(2010, 12, 31);
DateTime date2 = new DateTime(2010, 1, 1);

TimeSpan timeSpan = date2 - date1;
Console.WriteLine(timeSpan.TotalDays);

This program produces the output: -364. So it should perfectly work! One question: Did you use DateTime[] for the dates-array?

BTW: days > 10 does not check if days is zero.

Comments

0

If we assume your code looks exactly like that, and the dates array is correctly populated, then there is nothing wrong here that would cause days to be exactly zero. Maybe check that your dates array has the correct values in it? Barring that, post more code?

Comments

0

Either do this:

System.TimeSpan span = dates[0] - dates[1]; 
int days = Math.Abs((int)span.TotalDays);

if (days > 10)
{
    throw new Exception("Over 10 days");
}

Or this:

System.TimeSpan span = dates[1] - dates[0]; 
int days = (int)span.TotalDays;

if (days > 10)
{
    throw new Exception("Over 10 days");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.