C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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 )
{
// . . .
}
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:
}