question

RockHitman-2461 avatar image
0 Votes"
RockHitman-2461 asked AgaveJoe commented

find current age

Hi, I will be getting from source the date format as string mm/dd/yyyy ("11/04/1920").
I would like to know is there any easy logic to find out the Age.
I would like to know if the Age is >=16

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Try...

 bool isOver16 = DateTime.Now.AddYears(-16) >= DateTime.Parse("11/04/1920", CultureInfo.GetCultureInfo("en-US"));

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 commented

Check this method:

 string example = "11/04/1920";
 DateTime date = DateTime.Parse( example, CultureInfo.GetCultureInfo( "en-US" ) );
 DateTime today = DateTime.Today;
 int age = today.Year - date.Year;
 if( date.AddYears( age ) > today ) age -= 1;
    
 if( age >= 16 )
 {
    // . . .
 }


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, this below line of code is throwing error...can you pls clarify ? age-=1

if( date.AddYears( age ) > today ) age -= 1;


I just need to return the Age...thats it

0 Votes 0 ·

I do not know what error it is throwing.

0 Votes 0 ·
GeekDotnet avatar image
0 Votes"
GeekDotnet answered

Step 1: Create a method to Check the Age

public static int GetAge(DateTime dob)
{

 return DateTime.Now.AddYears(-dob.Year).Year;  

}

Step 2: Call the method

    string inputDate="11/04/1920";

    if(GetAge(Convert.ToDateTime(inputDate))>=16)
     {
      // TODO:
     }
     else
     {
        //TODO:
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.