1

How to useDateTimePicker in an If else condition because I would like to trap this problem. If a user types his birthday 01/01/2003 the system must prevent it from proceeding.

Even if this code works, it didn't also allow someone to be born on 01/01/1997 to be added.

Code:

ElseIf datepickerBirthday.Value < Date.Now Then
    MessageBox.Show("Minors are not allowed, according to the law",
                    "XYZ Auto Repair System",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error)`
Bugs
  • 4,491
  • 9
  • 32
  • 41
Alastair23
  • 31
  • 9
  • Maybe this question will help you: https://stackoverflow.com/questions/618878/how-to-compare-just-the-date-part-and-not-the-time-of-two-dates – muffi May 04 '17 at 06:39
  • You can add/subtract years etc. from a datetime. So the first step I would make is to define the minimum birthday the user must have to be 18 (21 in your country?): `Dim minbirthday = DateTime.Now.AddYears(-18)`. Then do your check: `If datepickerBirthday.Value < minbirthday Then ...` – Alex B. May 04 '17 at 06:45
  • Okay, but how can i apply this. Do i need to create a variable for date? – Alastair23 May 04 '17 at 06:46
  • Alex, Thank you! – Alastair23 May 04 '17 at 06:46

3 Answers3

1

Examples are fine but you need to actually tell us what the rule is. You say:

If a user types his birthday 01/01/2003 the system must prevent it from proceeding.

OK, why? What rule is it breaking that you need to catch it? Based on this:

Minors are not allowed, according to the law

I'm assuming that what you actually want, rather than getting the age of the employee, is simply to check whether the employee is at least 18 years old. If that's what you wanted then that's what you should have said. We shouldn't have to work out stuff like that. If that's the case then you need to check whether the Value is less than today's date minus 18 years, not the current date and time, i.e.

ElseIf datepickerBirthday.Value.Date < Date.Today.AddYears(-18) Then

Note that the code provided is going to tell you if the birth date specified makes the person 18 or over. If you want under 18 then invert the condition.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Indeed, as i'm basing this on a scenario when a user accidentally types the date mentioned above. – Alastair23 May 04 '17 at 06:55
  • Thanks, but this what happens next [link](http://i.imgur.com/RMLxVyt.png) – Alastair23 May 04 '17 at 07:11
  • It's okay, and i've make some changes with the solution ElseIf DateTime.Today.Year - datepickerBirthday.Value.Year < 18 = True Then MessageBox.Show("Minors are not allowed, according to the law", "XYZ Auto Repair System", MessageBoxButtons.OK, MessageBoxIcon.Error) – Alastair23 May 04 '17 at 10:41
  • @Alastair23, that will not work. If you consider a wide range of test cases then you'll see why. – jmcilhinney May 04 '17 at 10:57
  • You're seriously asking me that in a comment on the answer that I've already provided? – jmcilhinney May 04 '17 at 11:04
1

Try this

ElseIf DateTime.Today.Year - datepickerBirthday.Value.Year < 18 Then
 MessageBox.Show("Minors are not allowed, according to the law",
                "XYZ Auto Repair System",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error)   
Sowndarya
  • 97
  • 1
  • 15
  • Thank you so much! and there are some changes in your answer as it becomes like this ElseIf DateTime.Today.Year - datepickerBirthday.Value.Year < 18 = True Then MessageBox.Show("Minors are not allowed, according to the law", "XYZ Auto Repair System", MessageBoxButtons.OK, MessageBoxIcon.Error) – Alastair23 May 04 '17 at 10:36
  • That code will indicate that someone is 18 for the entire year of their 18th birthday, even if they were born on Dec 31. – jmcilhinney May 04 '17 at 11:15
0

Thanks to jmcilhinney and Sowndara and here is the solution of this problem

ElseIf DateTime.Today.Year - datepickerBirthday.Value.Year < 18 = True Then
            MessageBox.Show("Minors are not allowed, according to the law", "XYZ Auto Repair System", MessageBoxButtons.OK, MessageBoxIcon.Error)
Alastair23
  • 31
  • 9
  • That code will indicate that someone is 18 for the entire year of their 18th birthday, even if they were born on Dec 31. Also, the comparison to `True` is pointless because the `<` operator already returns a `Boolean`. – jmcilhinney May 04 '17 at 11:16