Here are two methods in C# that calculate age from a 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")));