0

Hello so I have this condition:

if (DateTime.Now.Subtract(dateTimePicker_Doc_BirthDate.Value).Days/(365)<18)
{
    this.errorProvider1.SetError(this.dateTimePicker_Doc_BirthDate, "Atleast 18 years old");
    valid = false;
}

And I want to make another that contains graduation of this person and set restriction that at the time when he graduat must be atleast 18 years older.

I have tried this: but it does not work

if (DateTime.Now.Subtract(dateTimePicker_Doc_Graduation.Value).Days / (365) < 18 + (18))
{
    this.errorProvider1.SetError(this.dateTimePicker_Doc_Graduation, "Atleast 18");
    valid = false;
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    Side note: It's a problem with *leap years*. When a person born in `29 Feb 2004` will be `18` years old? `1 Mar 2022` or `28 Feb 2022`? – Dmitry Bychenko Feb 27 '20 at 08:42
  • `29 Feb 2076`? :D – yaakov Feb 27 '20 at 08:43
  • 1
    Years aren't uniformly 365 days long. If you want to ensure that they're 18 years old, better to subtract 18 years from today's date and ensure that the DOB is earlier. I'm not sure what you're wanting to check with the graduation date, you've not explained very well what the requirement is. – Damien_The_Unbeliever Feb 27 '20 at 08:44
  • DateTime from = new DateTime(1995, 1, 1); DateTime now = DateTime.Now; var yearold = Math.Round((now - from).TotalDays / 356); – Phat Huynh Feb 27 '20 at 08:46
  • "Graduation is the successful completion of a course of study at a university, college, or school, for which you receive a degree or diploma". So the term "graduation date" is unclear by it's meaning. – Luuk Feb 27 '20 at 08:48
  • @PhatHuynh leap years. Different calendars. This doesn't work – Panagiotis Kanavos Feb 27 '20 at 08:48
  • At the very least, you should use [Calendar.AddYears](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.calendar.addyears?view=netframework-4.8). The `DateTime` type *doesn't* have `AddYears` or even `AddMonths` on purpose. There are leap years to consider and different countries can sue different calendars – Panagiotis Kanavos Feb 27 '20 at 08:52
  • @DmitryBychenko, That's a trick question.. It depends of the legislation computing the age. Not only the country, for example New zealand use 28 for driver license and 1rst for everything else. – xdtTransform Feb 27 '20 at 09:31
  • Does this answer your question? [How do I calculate someone's age in C#?](https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c) – xdtTransform Feb 27 '20 at 09:42

1 Answers1

1

The main problem here is leap year. When a person born in 29 Feb 2004 will be 18 year old? There are two possible answers:

https://www.buzzfeed.com/lanesainty/leap-year-birthday-teenager-court-ruling

  1. 28 Feb 2022 (New Zealand for driving license; see xdtTransform's comment)
  2. 1 Mar 2022 (Australia)

In a simple case, we can just add 18 year to the birthDate and see if 18th birth day is before or after today. For 29 February we may well have to add 1 day as well:

 DateTime birthDate = dateTimePicker_Doc_BirthDate.Value.Date;
 DateTime today = DateTime.Today;
 // 0 for 28 Feb 2022; 1 for 1 Mar 2022 
 int leapPolicy = 1;

 if (birthDate.AddYears(18) <= today || // 18th birthday before today or today
     birthDate.Day == 29 &&   
     birthDate.Day == 2 && 
     birthDate.AddYears(18).AddDays(leapPolicy) == today) {

   // At least 18 years old
 } 

Please, note, that year is not 365 but 365.2425 days (Gregorian calendar) that's why you can't put it as ...Days / 365...

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215