How to Calculate Age in Year in ASP.NET

Here I am giving two function to return age after getting the DOB.

Function1

public int get_age(DateTime dob)

{

int age = 0;

age = DateTime.Now.Subtract(dob).Days;

 

age = age / 365;

return age;

}

Function 2

public int get_age2(DateTime dob)

{

    int age = 0;

    age = DateTime.Now.AddYears(-dob.Year).Year;

    return age;

}

Now call these functions

Response.Write(get_age(Convert.ToDateTime("6/20/1993")));
Response.Write(get_age2(Convert.ToDateTime("6/20/1993")));

Ebook Download
View all
Learn
View all