Minimum age validation in C#

  • Thread starter Thread starter Jens Jensen
  • Start date Start date
J

Jens Jensen

How do i verify that an age entered as dd-mm-yyyy is minum 21 today in C#?

Any suggestion will be highly appreciated.

JJ
 
Hi JJ,

DateTime d = DateTime.Parse(date);
if(d <= DateTime.Now.AddYears(-21))
// over or equal to 21 years
 
cannot read your question well..

Age entered as dd-mm-yyyy ??? probably date of birth

if yes..

then load it to a datetime object
get the today date loaded to a date time object
get the difference to a timespan object and it have the property you are
looking for, years, months, days, hours etc

Nirosh.
 
DateTime d = DateTime.Parse(date);
if(d <= DateTime.Now.AddYears(-21))
// over or equal to 21 years


I like this solution.
Many thanks
JJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top