0

I want to select a date from a date time to be in a specific time range. I want to select a user's birthday in my datetime picker. I want to add only the users of age between 18-100 years. I want to check whether selected value of my datetime picker is greater than 18 years from today's date and selected value of date time picker less than 100 years from today's date. I tried this, but it gives me wrong output.

        if (dtpDOB.Value<DateTime.Today.AddYears(-18)&& dtpDOB.Value>DateTime.Today.AddYears(-100))
        {
            //
        }
6
  • 2
    you can set the properties of datetime picker min and max date. Commented Jan 31, 2015 at 12:00
  • @Tbseven Wrong answer. According to my knowledge min and max dates are fixed dates. If system runs today that can be true. But it changes in another time system runs right? Does it check 18 years back from today's date? No right? It checks values to the given date from today right? One person, who is not 18 to the selected min date can be 18, to the already set min date next time system runs right? Commented Jan 31, 2015 at 12:05
  • Can you describe what "wrong output" your code gives? Commented Jan 31, 2015 at 12:07
  • 1
    dateTimePicker1.MinDate = DateTime.Today.AddYears(-18); so the min date is always today least 18 Commented Jan 31, 2015 at 12:09
  • @JeppeStigNielsen I have set an error message, what i meant by wrong output is though I select today's date it does not give me the expected error. Commented Jan 31, 2015 at 12:15

2 Answers 2

1

Simple Boolean conditions can be confusing sometimes (I also submitted a wrong answer at first but immediately deleted it). Your condition was correct for determining if the date was OK:

if (dtpDOB.Value < DateTime.Today.AddYears(-18)
    && dtpDOB.Value > DateTime.Today.AddYears(-100))
{
    // the date entered was valid!
}

You can put an else block after that and handle the unwanted input there.

Note that you check for the "left" endpoint of the interval last. Today minus 100 years is the earlier of the two points in time.

I think you wanted to express the negation. That is like this:

if (!(dtpDOB.Value < DateTime.Today.AddYears(-18)
    && dtpDOB.Value > DateTime.Today.AddYears(-100)))
{
    // the date entered was bad, outside permitted range
}

or equivalently, by de Morgan's law:

if (dtpDOB.Value >= DateTime.Today.AddYears(-18)
    || dtpDOB.Value <= DateTime.Today.AddYears(-100))
{
    // the date entered was bad, outside permitted range
}

Here I used the "algebraic" fact that

!(x < b && x > a)

is logigally equivalent to

x >= b || x <= a

where the "strict" < reverses and becomes non-strict upon negation.

You should be sure what behavior you want if today is actually the 18th or 100th birthday of the user.

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

Comments

0

I don't think your if statement will ever be true.

Try using OR rather than AND:

    if (dtpDOB.Value<DateTime.Today.AddYears(-18) || dtpDOB.Value>DateTime.Today.AddYears(-100))
    {
        // show error
    }

1 Comment

The answer is wrong as it stands now. (Don't worry, I was about to make the same wrong answer.) Any date value is either earlier than 1997-January-31 or later than 1915-January-31, or both.

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.